javascript regexp surprise [duplicate] - javascript

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.

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>/)

regex to retrieve specific url from text [duplicate]

This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 7 years ago.
An API I use returns this text:
<http://192.168.1.10:8080/longUrl>; rel="recording-session",
<http://192.168.1.10:8080/realLongUrl>; rel="h264-session-sdp",
<http://192.168.1.10:8080/realLongDifferentUrl>; rel="h264-session-sdp",
<rtp://239.1.1.18:5006>; rel="destination-high",
<rtp://239.1.1.17:5006>; rel="destination-low"
I'm trying to retrieve the first URL that is followed by ; rel="h264-session-sdp.
So in this case that would be: http://192.168.1.10:8080/realLongUrl
I've been fiddling around trying to modify examples found here on SO, but just can's seem to get it right.
try this one /([^<]+)(?:>; rel\=\"h264\-session\-sdp\")/
Selects the text inbetween the greater then and less then characters:
?<=\<)(.*?)(?=>)
https://regex101.com/r/oS5sX6/1
And if you wanted to select the urls on multiple lines, add the g and m modifiers:
/(?<=\<)(.*?)(?=>)/gm
https://regex101.com/r/oS5sX6/2
Try this:
/(?<=\<)(.*?)(?=\>)>; rel="h264-session-sdp"/

JavaScript Regular Expression nothing to repeat [duplicate]

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)

Identify a URL Using Regular Expressions [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Checking for a valid url using Javascript Regular Expressions
PHP validation/regex for URL
I have a if statement that will check if the user entered a URL(HTTP Protocol only), like this:
if(/^regexp/.test(url))
But how should be this regular expression to check if the text is a URL or not?
I believe this little function might help:
function isURL(string){
regEx = /(\b(https?|ftp):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/gim;
return regEx.test(string));
}
Let us know if it worked out!
W.

Categories