Behavior of Regex in javascript [duplicate] - javascript

This question already has answers here:
escaping question mark in regex javascript
(4 answers)
Closed 6 years ago.
I am new to javascript and have not much idea about regular expressions. Please help me in understanding the following code.
var r = new RegExp("^.*?https://www\\.facebook\\.com/servlet/SignOn.*$","i");
var content = "https://www.facebook.com/servlet/SignOn?msg=You+are+not+authorized+to+vie…r+level+of+authority.&cm_sp=TopNav-_-servlet-_-MMM&goto=MembersMainMenu%3F";
console.log(content.search(r));// It gives me 0
But when I change the regex to
var r = new RegExp("^.*?https://www\\.facebook\\.com/servlet/SignOn?msg=.*$","i");
console.log(content.search(r));// It gives me -1 , why??

n? means n is optional you need n\?

Related

Why are these different way of declaring regular expressions not producing the same result? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 1 year ago.
Here's the code that declares the same pattern in two different ways
let reg = new RegExp("\d+"); // 1st way
let reg2 = /\d+/; // 2nd way
let string = "one two 100";
console.log(reg.test(string)); // false
console.log(reg2.test(string)); // true
I was reading the book "Eloquent JavaScript" and they said these two ways of declaring regex are equivalent but I don't know why the code above is producing different results.

How to get URLinformation after question mark? [duplicate]

This question already has answers here:
how to get url value after question mark in javascript
(2 answers)
Closed 3 years ago.
I have this simple code:
Subscribe today
when the user press on the a tag it takes him to the page subsc.php?English.
My question is how can i get the information after the ? (which is "English" in this example)?
You can try with Regex Positive Lookbehind /(?<=\?).$*/.
var url = '/subsc.php?English';
var r = url.match(/(?<=\?).*$/)[0];
console.log(r);

Regex after first sign [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
I'm gonna to write regex or other expression to get coordinates after '='.
My example:
var cords = https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac=
I want to get 50.082961,19.966860
I know that I could use slice but I think I could write it better with regex.
Simple base for this example: \=(.[0-9]) What's next?
Try this center\=(\d+\.\d+,\d+\.\d+)&
var val = 'https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac='.match(/center\=(\d+\.\d+,\d+\.\d+)&/)[1]
console.log(val)
But as other's have commented, you likely shouldn't be using regex for this purpose

Javascript split number and string and keep both [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 6 years ago.
I have a string
var houseNumber = '13a';
I want to split the addition from the number so I can keep it in a other field.
How can I split this value without losing the number or the addition? At the end I would like to have 2 fields with the following type:
var houseNumber = 13; //Number
var addition = 'a'; //String
There are many questions about this, but I can't find one where both values has to be saved. That's why I created a new question.
Use the following code
var string_a = "jkjkhj89898";
var x = string_a.match(/[^\d]+|\d+/g);
console.log(x)
working fiddle.
Thanks

Replacing “index” array string using Javascript regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 6 years ago.
with the following code can get the 1 in the string.
var match = /[0-9]+/.exec('[1][2]');
console.log(match);
How do i get the 2 and not the 1 ?
Try with this regex
console.log(/(\[[0-9]\])+/.exec('[1][2]'));
I think it's only a matter of escaping the square brackets

Categories

Resources