JavaScript Regex Not Working

Javascript RegEx Not Working

Because you're creating your regular expression from a string, you have to double-up your backslashes:

var regEx = new RegExp("^(0[1-9]|1[0-2])/\\d{4}$", "g");

When you start with a string, you have to account for the fact that the regular expression will first be parsed as such — that is, as a JavaScript string constant. The syntax for string constants doesn't know anything about regular expressions, and it has its own uses for backslash characters. Thus by the time the parser is done with your regular expression strings, it will look a lot different than it does when you look at your source code. Your source string looks like

"^(0[1-9]|1[0-2])/\d{4}$"

but after the string parse it's

^(0[1-9]|1[0-2])/d{4}$

Note that \d is now just d.

By doubling the backslash characters, you're telling the string parser that you want single actual backslashes in the string value.

There's really no reason here not to use regular expression syntax instead:

var regEx = /^(0[1-9]|1[0-2])\/\d{4}$/g;

edit — I also notice that there's an embedded "/" character, which has to be quoted if you use regex syntax.

Javascript Regex not working on script but working on console

The problem is because your RegExp is not initialized properly.

You can either do:

// Note the \\ to escape the backslash
var reg = new RegExp('^\\d{4}$');

Or

var reg = new RegExp(/^\d{4}$/);

Replacing with javascript regex not working

You have to use the proper regexp delimiter, the /.

You also need to remove the ^ and the $ otherwise it match on the full line only.

Last but not least,
you want
replacedText = daText.replace(daRex, '');
and not
daText = replacedText.replace(daRex, '');

var daRex = /(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})/daText = "Testing REGEX 123 4387000500875798";replacedText = daText.replace(daRex, '');console.log(replacedText)

Validate.js ReGex not working as expected

Use

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,20}$/

EXPLANATION

--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[!@#$%^&*] any character of: '!', '@', '#', '$',
'%', '^', '&', '*'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.{8,20} any character except \n (between 8 and 20
times (matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

javascript regular expression to not match a word

This is what you are looking for:

^((?!(abc|def)).)*$

The ?! part is called a negative lookahead assertion. It means "not followed by".

The explanation is here:
Regular expression to match a line that doesn't contain a word

Regex not working as expected in JavaScript

Your problem is that JavaScript is viewing all your escape sequences as escapes for the string. So your regex goes to memory looking like this:

^(https?://)?([da-z.-]+).([a-z]{2,6})(/(w|-)*)*/?$

Which you may notice causes a problem in the middle when what you thought was a literal period turns into a regular expressions wildcard. You can solve this in a couple ways. Using the forward slash regular expression syntax JavaScript provides:

var urlexp = /^(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?$/gi

Or by escaping your backslashes (and not your forward slashes, as you had been doing - that's exclusively for when you're using /regex/mod notation, just like you don't have to escape your single quotes in a double quoted string and vice versa):

var urlexp = new RegExp('^(https?://)?([da-z.-]+)\\.([a-z]{2,6})(/(\\w|-)*)*/?$', 'gi')

Please note the double backslash before the w - also necessary for matching word characters.

A couple notes on your regular expression itself:

[da-z.-]

d is contained in the a-z range. Unless you meant \d? In that case, the slash is important.

(/(\w|-)*)*/?

My own misgivings about the nested Kleene stars aside, you can whittle that alternation down into a character class, and drop the terminating /? entirely, as a trailing slash will be match by the group as you've given it. I'd rewrite as:

(/[\w-]*)*

Though, maybe you'd just like to catch non space characters?

(/[^/\s]*)*

Anyway, modified this way your regular expression winds up looking more like:

^(https?://)?([\da-z.-]+)\.([a-z]{2,6})(/[\w-]*)*$

Remember, if you're going to use string notation: Double EVERY backslash. If you're going to use native /regex/mod notation (which I highly recommend), escape your forward slashes.

Multiple RegEx not working with conditions

A regular expression with the g (global) flag keeps track of the position of the last match, and keeps searching from the next position.

So this means that the first match will return the position on the first number, and then the second match will fail because instead of restarting from the start it will start from the position of the first match and miss.

The fix is to simply remove the g from your original expression like this

/[0][4-5][0-9]{8}/;

However, I also see that your expression might give false positives. For example, if you add random characters before and after the actual number, it will still match.

E.g.

firstPhone = 'WRONG0432779680WRONG';

Will still return true. If you're using this for validating a phone number format, you need to restrict the regex to the whole string passed. You can do so by using ^ at the beginning and $ at the end, to say this is the whole string.

So the regex should look like

const mobileRegEx = /^[0][4-5][0-9]{8}$/;

Here's the final code

const mobileRegEx = /^[0][4-5][0-9]{8}$/;
const firstPhone = '0432779680';
const secondPhone = '0543000987';
if(mobileRegEx.test(firstPhone) && mobileRegEx.test(secondPhone)){
console.log(firstPhone, secondPhone);
}

Regex not working for multiple characters

You may use

const regexM = /[\\`*_{}[\]()#+.!|-]/g;
var input = `B *H*C**AB**`;
var inputString = input.replace(regexM, "\\$&");inputString = inputString.replace(/\r?\n/g, "<br/>");console.log(inputString);// => B <br/>\*H\*<br/>C<br/>\*\*AB\*\*


Related Topics



Leave a reply



Submit