JavaScript Regular Expression nothing to repeat [duplicate] - javascript

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 7 years ago.
I'm using the following JavaScript code to create a regular expression to match a UK mobile number:
new RegExp("(\+44|0)7\d{9}", 'g');
However, I get an error in the console log saying:
Uncaught SyntaxError: Invalid regular expression: /(?:+44|0)7d{9}/:
Nothing to repeat
Similar questions on StackOverflow point to a missing escaped character, but mine seem to be fine.
I have also tried without the global flag.
Help would be greatly appreciated.

You can create RegExp by using short method
var a = /(\+44|0)7\d{9}/g
Its must works good)

Related

Am confused about the functions of caret(^) in Regular expresssions. What does it mean here [^'] and what does it mean here [^01] [duplicate]

This question already has answers here:
Negating specific characters in regex
(4 answers)
Reference - What does this regex mean?
(1 answer)
Closed 20 days ago.
I have a regular expression:
/'([^']*)'/
Am finding it hard to understand how it works. The function of the caret here confuses me.
Unlike this regex:
/[^01]/ : i understand the caret here is an inverter which means the search should return true for any input input value that is different from 01.
let quotedText = /'([^']*)'/;
console.log(quotedText.exec("She said 'hello'"));
The console: ["'hello'", "hello"]
I do understand how the regexpression(quotedText) finds hello. What if the statement was longer with more words in quote. Like:
("She said 'hello' and he responded 'Hi', 'do you need my help'").
Would the exec method find all the words or sentences in quotes?.
I am also very confused about the function of caret^ here. Is it inverting?? Or is it showing where the exec methods starts looking from. Whats the difference between [^']* and [^01]. Does the function of caret change based on the method. Does caret(^) you see work differently when used with test method or exec method?. does Caret behave differently when in square brackets?

Javascript regex throwing syntax error in edge [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
I have a simple regex I'm using and that works perfectly in chrome but edge throws a syntax error, ths is the line :
var html=text.match(/^<div.+\/div>$/ims);
I don't see the problem.
Because /s flag is not supported, use:
var html=text.match(/^<div[\s\S]+\/div>$/im);
Basically you want to match all characters with a new line character
You can this regex please:-
text.match(/^<div>.+\n*.*<\/div>/)

Javascript Regex not working with Node [duplicate]

This question already has answers here:
Regex works on browser but not in Node.js
(2 answers)
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I have the following regex that works when used on the chrome browser:
file.match(/(?<=__\()(.*)(?=\))/g);
But it seems to throw an error when using Node on my machine:
SyntaxError: Invalid regular expression: /(?<=__\()(.*)(?=\))/: Invalid group
Any ideas why this might be? Also I am trying to match string that appear in groups like this:
__("string to match");
const file = "__('string to be extracted');";
const strings = file.match(/(?<=__\()(.*)(?=\))/g);
console.log(strings);

javascript regexp surprise [duplicate]

This question already has an answer here:
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 5 years ago.
I tried the following JavaScript in developer console of Chrome:
s = "mysessionId=PsGymRfxWIQG9gjNGgRlKw"
s.match("mysessionId=([^\s\;]+)")
A little surprised by the result:
["mysessionId=P", "P"]
I had expected that the () in regexp will match the entire "PsGymRfxWIQG9gjNGgRlKw", instead, it only matched the first character "P".
When I tried the regexp in perl, it does match the entire sessionId.
Any idea why?
I should have used // instead of "".
s.match(/mysessionId=([^\s\;]+)/)
Had to laugh the moment when I figured it out.

JS regular expression multi-line [duplicate]

This question already has answers here:
How to split a long regular expression into multiple lines in JavaScript?
(11 answers)
Closed 8 years ago.
This one seems like it has a very simple answer, yet I can't find it anywhere. I have a regular expression that is quite large, how do I put in some line breaks in the expression itself so I don't have to keep scrolling horizontally through the code to see it all?
I don't normally use word-wrap, and the IDE I'm using doesn't even offer it anyway.
A line break in a string would normally be a \ at the end of the line :
var mystring "my string \
is on more \
than one line";
var re = new RegExp(mystring, "gim");
You could use RegExp and .join() to convert and concat a string.
var myRegExp = RegExp(['/^([a-zA-Z0-9_.-])+'
,'#([a-zA-Z0-9_.-])+'
,'\.([a-zA-Z])+([a-zA-Z])+/'].join(''));
The answer has been linked to here as well.
How to split a long regular expression into multiple lines in JavaScript?

Categories