This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 2 years ago.
console.log('\d' === 'd'); // true
Character 'd' is not a special character, why javascript want to slice the escape notation.
It's better to keep the escape notation in my view.
When I want to fully match string-'\d' using regular expression, it just impossible!
Taking the following code as an example.
console.log(RE.test('\d')); // it should log true
console.log(RE.test('d')); // it should log false
Unfortunately, you just cannot figure out a regular expression pattern.
You have no reason to escape d in a string and JavaScript ignores it. If you need \d you need to escape the escape character: \\d.
See also Why do linters pick on useless escape character?
\d has a special meaning in regular expressions (a digit character), but also in strings (escaped 'd' character, which is exactly like 'd').
Any / creates an escape sequence in a string. Some are "useful" (\n === new line) and some arguably useless (`'\d' === 'd').
If you want the regex \d, you could
1 - use a regex literal instead : /\d/
2 - escape the \ in the string : '\\d', so that the string containing the two characters \ and d is correctly understood by Javascript.
Related
This question already has an answer here:
Escape string for use in Javascript regex [duplicate]
(1 answer)
Closed 3 years ago.
I have a regex that takes a template literal and then matches it against a CSV of conditions and links.
const regex = new RegExp(`^${condition},\/.+`, 'gi');
For example, the variable Sore throat would match
'Sore throat,/conditions/sore-throat/'
I've come across an issue where the template literal might contain brackets and therefore the regex no longer matches. So Diabetes (type 1) doesn't match
'Diabetes (type 1),/conditions/type-1-diabetes/'
I've tried removing the brackets and it's contents from the template literal but there are some cases where the brackets aren't always at the end of the string. Such as, Lactate dehydrogenase (LDH) test
'Lactate dehydrogenase (LDH) test,/conditions/ldh-test/'
I'm not too familiar with regex so apologies if this is simple but I haven't been able to find a way to escape the brackets without knowing exactly where they will be in the string, which in my case isn't possible.
You are trying to use a variable that might contain special characters as part of a regex string, but you /don't/ want those special characters to be interpreted using their "regex" meaning. I'm not aware of any native way to do this in Javascript regex - in Perl, you would use \Q${condition}\E, but that doesn't seem to be supported.
Instead, you should escape your condition variable before passing it into the regex, using a function like this one:
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
I am creating a binary calculator and I would like to create a regular expression to match the operands and a single operator. The regular expression that I've created works correctly for everything except the backslash \ character. Here is my regular expression:
re = /^([10]+)([\+\-\*\\]{1})([10]+)/;
re.test('11001+1000');
// true
re.test('11001-1000');
// true
re.test('11001*1000');
// true
re.test('11001\1000'); // I WOULD THINK THIS WOULD WORK
// false
re.test('11001\\1000'); // I WOULD THINK THIS WOULD FAIL
// true
re.test('11001++1000');
// false
Can anyone advise me on what I am doing wrong?
When you create string in JS with '\' - it is treated as escape character. Therefore '11001\\1000' will become '11001\1000', but creation of '11001\1000' will become "11001#0".
You could try it:
let rr = '11001\1000';
console.log(rr);
Therefore '11001\\1000' is your case. And if you have string that already contains '11001\1000' - it will work.
re.test('11001\1000');
Javascript does not see that string as "11001\1000" like you do. It sees it as the sequence of characters "1", "1", "0", "0", "1", "\100", "0". Whenever a backslash appears in a javascript string, it is interpreted as an escape symbol combined with one or more following characters. If the following character is a digit, it reads in up to 3 characters, and treats them as a character code in octal. The character code in octal 100 converts to the # symbol, so the end result of that string is "11001#0".
However, if the character immediately following the backslash is another backslash, it realizes that you mean a literal backslash, and so you get the following:
11001\\1000
becomes
1 1 0 0 1 \\ 1 0 0 0
becomes
11001\1000
which is what you want.
Division sign is / not \
re = /^[10]+[-+*\/][10]+/;
console.log(re.test('11001+1000'));
console.log(re.test('11001-1000'));
console.log(re.test('11001*1000'));
console.log(re.test('11001/1000'));
console.log(re.test('11001\\1000'));
console.log(re.test('11001++1000'));
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
Here is an attempt to remove any excessive blank lines in string.
I'm trying to understand why second approach doesn't workfor lines which contains whitespace.
Demo.
var string = `
foo
bar (there are whitespaced lines between bar and baz. I replaced them with dots)
....................
.......................
...........
baz
`;
// It works
string = string.replace(/^(\s*\n){2,}/gm, '\n');
// Why it doesn't work?
var EOL = string.match(/\r\n/gm) ? '\r\n' : '\n';
var regExp = new RegExp('^(\s*' + EOL + '){2,}', 'gm');
string = string.replace(regExp, EOL);
alert(string);
Your \s needs to be changed to \\s. Just putting \s is the same as s.
In strings (enclosed in quotes), the backslash has a special meaning. For example, \n is the newline character. There are a couple of others that you may or may not have heard of, e.g. \b, \t, \v. It would be bad language design choice to make only a few defined ones special, and consider the non-existent \s to be an actual backslash and an s, because it would be inconsistent, a source of errors, and not future-proof. That's why, when you want to have a backslash in a string, you escape the backslash to \\.
In your first example, you use / characters to delimit the regular expression. This is not considered a string bound by the above rules.
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 2 years ago.
Have found this out by accident and have no idea what's the reason.
// Results in "Syntax error in regular Expression".
var re = RegExp('\\');
I know that the constructor-function expects a string as parameter. And that the backslash is used within strings to escape characters with special meaning. I know that I have to escape characters like \d to \\d .
So therefore: The right backslash should the interpreted as some normal character.
Instead it throws an error. Why?
Can anyone explain this to me?
\ is used to escape \ in strings, so to get \d as you wrote you need to do \\d.
Also in regexp you need to escape \ with \\.
So you have two escape syntaxes that need to take place in regexps, using a single \\ will mean \ in regexp which is not correct, because it needs to be escaped.
So to workaround this you need double escape: \\\\ - this will be a regex looking for \.
The string literal '\\' creates a string containing nothing but a single backslash character, because within string literals the backslash is an escape character.
A single backslash character is not a valid regular expression.
If you want a regex that matches a single backslash then that needs to be escaped within the regex, so you need to do either:
re = /\\/;
// or
re = new RegExp('\\\\');
I believe the reason you are getting this error is that the effective regex which you are feeding into the JavaScript engine is a single backslash \.
The reason for this is that the first backslash escapes the second one. So you are putting in a literal backslash, which doesn't make any sense.
The backslash \ is the escape character for regular expressions. Therefore a double backslash would indeed mean a single, literal backslash. \ (backslash) followed by any of [\^$. ?*+(){} escapes the special character to suppress its special meaning.
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Extra backslash needed in PHP regexp pattern
(4 answers)
Regex to replace single backslashes, excluding those followed by certain chars
(3 answers)
Closed 7 years ago.
function trim(str) {
var trimer = new RegExp("(^[\\s\\t\\xa0\\u3000]+)|([\\u3000\\xa0\\s\\t]+\x24)", "g");
return String(str).replace(trimer, "");
}
why have two '\' before 's' and 't'?
and what's this "[\s\t\xa0\u3000]" mean?
You're using a literal string.
In a literal string, the \ character is used to escape some other chars, for example \n (a new line) or \" (a double quote), and it must be escaped itself as \\. So when you want your string to have \s, you must write \\s in your string literal.
Thankfully JavaScript provides a better solution, Regular expression literals:
var trimer = /(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+\x24)/g
why have two '\' before 's' and 't'?
In regex the \ is an escape which tells regex that a special character follows. Because you are using it in a string literal you need to escape the \ with \.
and what's this "[\s\t\xa0\u3000]" mean?
It means to match one of the following characters:
\s white space.
\t tab character.
\xa0 non breaking space.
\u3000 wide space.
This function is inefficient because each time it is called it is converting a string to a regex and then it is compiling that regex. It would be more efficient to use a Regex literal not a string and compile the regex outside the function like the following:
var trimRegex = /(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g;
function trim(str) {
return String(str).replace(trimRegex, "");
}
Further to this \s will match any whitespace which includes tabs, the wide space and the non breaking space so you could simplify the regex to the following:
var trimRegex = /(^\s+)|(\s+$)/g;
Browsers now implement a trim function so you can use this and use a polyfill for older browsers. See this Answer