I want to remove strings in the form of the following where some-text is a random text string.
$('#some-text').val();
I've tried various things but I think the $ sign is messing things up since it's used in regex.
You need to escape some characters.
Try this -
\$\('#[^']*'\)\.val\(\);
Try this regex by escaping special chars:
\$\(.*\).val\(\);
To avoid escaping the special characters, you can use \Q - \E pair to surround the part where you want the regex engine to interpret literally:
\Q$('\E<your-regex>\Q').val();\E
Replace <your-regex> with your regex to match the selector, or whatever it is.
Related
This is my first time trying to use a regex for deletion.
The regex:
/net=.+\.net/
as shown here matches a string that starts with net= some random characters and ends with .net
However, when using it in vim:
:g/net=.+\.net/d
I simply get Pattern not found: net=.+\.net
I am guessing that vim uses a slightly different format, or do I need to escape the characters =, . and + ?
:help pattern is your friend. In your case, you need to escape + or prefix your whole pattern with \v to turn it “verymagic”.
Do not escape =, it would turn it into the same thing as {0,1} in some regexp engine, namely a greedy optional atom matcher.
In regex is there a way to escape special characters in an entire region of text in PCRE syntax?
eg. hey+Im+A+Single+Word+Including+The+Pluses+And.Dots
Normally to match the exact string in regex I would have to escape every single + and . with /s in the above string. This means that if the string is a variable, One has to seek for special characters and escape them manually. Is there an easier way to do this by telling regex escape all special characters in a block of text?
The motivation behind this is to append this to a larger regex so even though there are easier ways to get exact matches they don't apply here.
Everything between \Q and \E meta characters are treated as literals in PERL Compatible RegExes (PCRE). So in your case:
\Qhey+Im+A+Single+Word+Including+The+Pluses+And.Dots\E
Other engines rarely support this syntax.
If it's python. you can use
re.escape(string) to get a literals string
import re
search = 'hey+Im+A+Single+Word+Including+The+Pluses+And.Dots'
text = '''hey+Im+A+Single+Word+Including+The+Pluses+And.Dots
heyImmASingleWordIncludingThePlusessAndaDots
heyImASingleWordIncludingThePlusesAndxDots
'''
rc = re.escape(search)
#exactly first line in text
print(re.findall(rc,text))
#line two and three as it will + as repeat and . as any char
print(re.findall(search,text))
-------- result -------------------
['hey+Im+A+Single+Word+Including+The+Pluses+And.Dots']
['heyImmASingleWordIncludingThePlusessAndaDots', 'heyImASingleWordIncludingThePlusesAndxDots']
I have a file, that contains spaces at the end of the line and they should be removed.
When I use the following command:
%s/\s+$//
Vim shows me an error that pattern is not found. What is wrong here?
NOTE: actually, I can use the %s/\s*$// command but I want to understand the root cause of the issue.
Vim shows me an error that pattern is not found
You need to escape the quantifier +.
:%s/\s\+$//g
You might also want to refer to Quantifiers, Greedy and Non-Greedy.
You can use it like this:
%s/ \+$//
OR:
%s/\s\+$//
As + needs to be escaped in vim regex.
Using the "very magic" mode with \v, you only need to escape alphanumeric specials:
%s:\v\s+%(//.*)?$::
(Here : is used as a separator, instead of /. You can use almost any non-alphanumeric ASCII character.)
another way to do it:
:%s/ *$//g
if you do not want to input '\' chars ;)
Is there way to pass some string to regexp and not worry about ecranisation of special chars.
For example I wont to find line which starts with words "\north+west\", as you can see "\n" and "h+" should be ecranised. So question is there some special combination to write text as it is?
/^\s+(<some special combination> \north+west\)\s+/i
or maybe you know function which can properly ecranise my text?
In PHP and Perl you can use \Q...\E delimiters to autoescape metacharacters inside regexp. Quoting the doc:
\Q and \E can be used to ignore regexp metacharacters in the pattern.
For example: \w+\Q.$.\E$ will match one or more word characters,
followed by literals .$. and anchored at the end of the string.
In addition to #raina77ow answer, when you use pcre via a language like PHP that needs pattern delimiters, you can't use the \Q...\E feature if your string contains the opening or the closing delimiter. For example, you can't write patterns like:
/\Qabc/def\E/
~\Qabc~def\E~
[\Qabc[def\E]
[\Qabc]def\E]
(\Qabc)def\E)
(\Qabc(def\E)
The only way is to use the preg_quote function and to put the delimiter (only if this one isn't already a special regex character) in its second parameter.
Can't work this one out, this matches a single star:
// Escaped multiply
Text = Text.replace(new RegExp("\\*", "g"), '[MULTIPLY]');
But I need it to match \*, I've tried:
\\*
\\\\*
\\\\\*
Can't work it out, thanks for any help!
You were close, \\\\\\* would have done it.
Better use verbatim strings, that makes it easier:
RegExp(#"\\\*", "g")
\\ matches a literal backslash (\\\\ in a normal string), \* matches an asterisk (\\* in a normal string).
Remember that there are two 'levels' of escaping.
First, you are escaping your strings for the C# compiler, and you are also escaping your strings for the Regex engine.
If you want to match "\*" literally, then you need to escape both of these characters for the regex engine, since otherwise they mean something different. We escape these with backslashes, so you will have "\\\*".
Then, we have to escape the backslashes in order to write them as a literal string. This means replacing each backslash with two backslashes: "\\\\\\*".
Instead of this last part, we could use a "verbatim string", where no escapes are applied. In this case, you only need the result from the first escaping: #"\\\*".
Your syntax is completely wrong. It looks more like Javascript than C#.
This works fine:
string Text = "asdf*sadf";
Text = Regex.Replace(Text, "\\*", "[MULTIPLY]");
Console.WriteLine(Text);
Output:
asdf[MULTIPLY]sadf
To match \* you would use the pattern "\\\\\\*".