How to escape asterisk in regexp? - javascript

I want to use the pattern *1*. I have tried \*1\*, but it doesn't work. Where is the problem?

You have to escape it with a backslash:
/\*1\*/
Otherwise, an unescaped * in a RegExp will mean: Match 0 or more of the Preceding Character Group.
Update:
If you use the RegExp constructor, do it this way:
new RegExp("\\*1\\*")
You have to double-escape the backslashes because they need to be escaped in the string itself.

need to use a backslash \ as the escape character in regexes.

Related

Javascript replace character with backslash

How do I replace + with \+ using the replace method?
console.log("abc+pqr".replace("+","\+"));
This gives the original string abc+pqr but I want abc\+pqr.
How do I get this?
You need to escape the backslash:
console.log("abc+pqr".replace("+","\\+"));

How to Remove double backslashes in a string to Single one?

My string is tel:\\99999999999. How i can replace '\' to '\' single? I want output like: tel:\99999999999.
Please see attached image backslashes not showing into question.
You should escape the slash twice:
"tel:\\99999999999".replace('\\\\', '\\');
You can simply use tel.replace(/\\\\/g, "\\").
Demo:
var tel ="\\99999999999";
console.log(tel.replace(/\\\\/g, "\\"));
You need to escape the \ character with another \, because it's an escape character in JavaScript, you can check JavaScript backslash (\) in variables is causing an error for further reading.
use .replace() to achieve what you want, str.replace("\\\\","\\")
var str = "tel:\\99999999999";
console.log(str.replace("\\\\","\\"))
You can do like this
"tel:\\99999999999".replace('\\\\', '');

Javascript RegExp and \\

I have the following expression: ^(0|1)\\1{1,}$.
Why it works only when I use it with new RegExp()?
// return always false
/^(0|1)\\1{1,}$/.test("000");
// it works!
var pattern = new RegExp("^(0|1)\\1{1,}$");
pattern.test("000");
Remove the second backslash:
/^(0|1)\1{1,}$/.test("000"); // true
The reason why that works is that backslashes are treated literally in regex literals, while they are escaped in strings (which is then escaped again in the regular expression).
For example:
new RegExp('\\\\').test('\\'); // true
/\\\\/.test('\\'); // false
new RegExp('\\\\').source; // '\\' - one backslash
/\\\\/.source; // '\\\\' - two backslashes
console.log('\\'); // shows one backslash
When you use RegExp, the regular expression will be treated as string. So, you have to escape the \ with \\. That escaping is not needed when you use it with /.../ form. So, simply remove the second \ in \\.
console.log(/^(0|1)\1{1,}$/.test("000"));
Output
true
Try this regex by stripping \\1 to \1:
/^(0|1)\1+$/.test("000");
When you construct RegExp object using a String you need double escaping:
1st by String object
2nd by Regex Engine
which is not needed here since you're directly constructing a regex using / and /

Match special characters including square braces

I want to have a regex for text field in ExtJs(maskRe) which matches all java code pattern
I've used
maskRe:/^[A-Za-z0-9 _=//~'"|{}();*:?+,.]*$/
I also want to include [,], but it seems /[, /], //[, //] is not working..
Any inputs please
The problem is you need to escape your forward slash. Change // to \/:
/^[A-Za-z0-9 _=\/~'"|{}();*:?+,.]*$/
However this regular expression does not match any Java code. Java code can contain almost any Unicode character. int møøse = 42; is valid Java.
To strip special characters from its magic powers you have to escape them, by putting backslash \ in front of character. I.e. to match [ you type \[.
And since backslash acts as special character as well, to match it literally, you escape it the same way: \\.
And since you used / as patter delimiter, you need to escape its occurrences within pattern:
/^[A-Za-z0-9 _=\/~'"|{}();*:?+,.]*$/
The way to escape regex meta-characters is using a backslash (\), not a forwards slash (/).
[,] should be \[,\]
// should be \/

Javascript -- regex strings

Simple question... I am just baffled how to write the notation.
Example: input='..."aaaa\"bbbb"...'
I need regex to grab the string ignoring nested quotations.
I guess it can start like: input=input.replace(/[^\\]"...
How can I say 'all characters until a " which is not preceded by a \' ?
Thanks!
"([^"\\]|\\.)*"
Inside the quotes can be (a) any character aside from a quote or backslash, or (b) any character if it's escaped with a backslash. Repeat.

Categories

Resources