Backslashes - Regular Expression - JavaScript

Backslashes - Regular Expression - Javascript

You should use a regular expression literal (/.../) instead of a string literal ('...' or "...") in the call to replace. Strings have their own interpretation of backslashes that kicks in before the regular expression constructor gets a crack at it, so you need an extra level of quoting.

Match one backslash, regular expression literal: /\\/

Match one backslash, regular expression in a string: '\\\\'

But in a regex literal, you also have to put backslashes in front of the forward slashes, since forward slashes are the delimiter for the whole thing:

path += arguments[i].replace(/(\\|\/)$|^(\\|\/)/, "") + "/";

Or, if you're married to the use of strings for some reason, this should also work:

path += arguments[i].replace("(\\\\|/)$|^(\\\\|/)", "") + "/";

As a side note, when your alternatives are single characters, (x|y) is overkillish; you can just use a character class ([xy]). In which case you get this:

path += arguments[i].replace(/[\\\/]$|^[\\\/]/, "") + "/";

path += arguments[i].replace("[\\\\/]$|^[\\\\/]", "") + "/";

Escaping Backslash in a RegExp String

Because you're creating the RegEx from a string literal, the double backslash is being interpreted as an escaped backslash within the string and you're winding up with this RegEx:

^[\w., #&/-]+$

This matches word characters, but backslashes are nowhere to be found.

The solution: escape both backslashes, and add one for the \w, resulting in six backslashes:

var regex = new RegExp("^[\\\\\\w., #&/-]+$");

Or even better, use a regular expression literal and only use three backslashes:

var regex = /^[\\\w., #&/-]+$/;

javascript regex backslash issue

If you intend the original to have literal backslashes in them, you need to escape them in your literal string notation, like this:

var testString = "one . one\\.two . one\\.two\\.three" ;

I will assume this is what you intended.

If the parts are always separated by space-dot-space, it is easier to just split the string by that.

In case this is not suitable for you (your general pattern is not always like that and/or you really need it to be a regular expression), you can use this variant of your regex:

/(\\\.|[^\s\.\\])+/g

var testString = "one . one\\.two . one\\.two\\.three" ;

console.log('input:', testString);

var result = testString.match(/(\\\.|[^\s\.\\])+/g);

// Show result:

for (var match of result) {

console.log('output:', match);

}

JavaScript RegExp : Why causes a double backslash ( \\ ) an error?

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.

What does a backslash mean when used in a regular expression?

Without [..] has a special meaing in regular expression. [xyz] matches x, y or z.
[^...] negate thats; [^xyz] matches any character that is not x, y, nor z.

But if [ is preceded by \, it loses its special meaning and matches [ literally.

Why do I have to add double backslash on javascript regex?

You could write the regex without double backslash but you need to put the regex inside forward slashshes as delimiter.

/^\w{3,}\s$/.test('foo ')

Anchors ^ (matches the start of the line boundary), $ (matches the end of a line) helps to do an exact string match. You don't need an i modifier since \w matches both upper and lower case letters.



Related Topics



Leave a reply



Submit