Regex Created via New Regexp(Mystring) Not Working (Backslashes)

Regex created via new RegExp(myString) not working (backslashes)

Your problem is that the backslash in a string has a special meaning; if you want a backslash in your regexp, you first need to get literal backslashes in the string passed to the regex:

new RegExp('\\b[\\d \\.]+\\b','g');

Note that this is a pretty bad (permissive) regex, as it will match ". . . " as a 'number', or "1 1...3 42". Better might be:

/-?\d+(?:\.\d+)?\b/

Sample Image

Note that this matches odd things like 0000.3 also does not match:

  • Leading +
  • Scientific notation, e.g. 1.3e7
  • Missing leading digit, e.g. .4

Also, note that using the RegExp constructor is (marginally) slower and certainly less idiomatic than using a RegExp literal. Using it is only a good idea when you need to constructor your RegExp from supplied strings. Most anyone with more than passing familiarity with JavaScript will find the /.../ notation fully clear.

Why my regular expression does not work in JavaScript? (It works in Java)

The test method is different from the matches method in Java.

RegEx.prototype.test() returns true whenever a match is found in the string, whereas Matcher.matches() returns true only if the whole string matches the pattern. So the JavaScript test() is more similar to the Java Matcher.find() than to Matcher.matches().

To make your Javascript regexes work, you need to add ^ and $ to assert the start and end of the string:

    var orderNumberRegExp = /^(?:(\d+)|(N\/A))$/    console.log(orderNumberRegExp.test("12345"));        var digitalOnly = /^\d+$/;    console.log(digitalOnly.test("12345"));    console.log(digitalOnly.test("12345ABC"));

JavaScript regex with escaped slashes does not replace

Your regex is perfect, and yes, you must escape slashes since JavaScript uses the slashes to indicate regexes.

However, the problem is that JavaScript's replace method does not perform an in-place replace. That is, it does not actually change the string -- it just gives you the result of the replace.

Try this:

myString = '/courses/test/user';
myString = myString.replace(/\/courses\/([^\/]*)\/.*/, "$1");
document.write(myString);

This sets myString to the replaced value.

VSCode deletes `\` on save from my regex pattern

Whether this will fix your problem or not, I'm not sure, but that regex should be:

const reg = new RegExp('\\.js$');

Since you're doing the regex as a string, you need to double your backslashes to represent a single backslash.

Or you could do:

const reg = /\.js$/;

It could be that VSCode is deleting the backslash the way you originally wrote this because it's an invalid escape.

Regex pattern not working in MySQL when I try to deny the dot

^ only means to exclude characters when it's at the beginning of a [...] character set. Outside that, it matches the beginning of the string.

If you want to match a non-dot, you should use [^.].

SELECT 'roger' REGEXP '[a-zA-Z][^.]'

But since this will match anywhere in the string, it won't work for excluding strings that contain .. Instead you should reverse the condition:

SELECT 'roger.' NOT REGEXP '[a-zA-Z]\\.'


Related Topics



Leave a reply



Submit