This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 3 years ago.
I am playing with regular expressions in Javascript and have stumbled across an unexpected behaviour. Take a look: https://jsfiddle.net/ft7h5cw0/
I am trying to replace .html-wrap string in a #my-html-wrapper-random .html-wrap .main-content with #my-html-wrapper-random .html-wrap. For some reason, the result after replacement is:
#my#my-html-wrapper-randomper-random .html-wrap .main-content instead of expeceted #my-html-wrapper-random #my-html-wrapper-random .main-content
What is the reason for this behaviour?
var testString = "#my-html-wrapper-random .html-wrap .main-content";
var rxString = ".html-wrap";
var rx = new RegExp(rxString);
var result = testString.replace(rx,"#my-html-wrapper-random"); // unexpexted result strinf
console.log(result);
Related
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Regular expression to match string starting with a specific word
(10 answers)
Regular expression - starting and ending with a character string
(3 answers)
Closed 3 years ago.
I am trying to pull out all the URLs from a text entered by the user by doing the following but am not able to get the desired result.
let regexp = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]+$/igm;
let str = "https://mysleepyhead.com http://skreem.io";
let array = [...str.matchAll(regexp)];
console.log(array);
Desired output would be
Array ['https://mysleepyhead.com', 'http://skreem.io']
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
Closed 4 years ago.
I'm trying to return how many times the string s.t() was found in a string, but I can't get the correct regex for this...
For example
var string = 'function(test) {s.t(); s.t(dsabf); s.t();}'
var re = new RegExp('s\.t\(\)', "g");
return re;
should return an array of 2 elements ['s.t()', 's.t()'] but instead it has 3 elements ['s.t', 's.t', 's.t']
I've also tried with ^s\t\(\)$ but this returns no match...
How can I fix my regex in order to make this work as expected?
This question already has answers here:
Regular expression for extracting a number
(4 answers)
Closed 6 years ago.
I have the following string:
PR-1333|testtt
I want to get the number 1333 using regular expressions in javascript. How can I do that?
This will look for numbers in your text.
var text = "PR-1333|testtt";
var number = text.match(/\d+/g); // this returns an array of all that it found.
console.log(number[0]); // 1333
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Parsing an URL in JavaScript
(3 answers)
Closed 8 years ago.
How to format a regular expression In JavaScript :
http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5
I want to get &pl= and all the digits after &pl=.
So the result would be &pl=1620 in this case.
Please help how to perform this??
Your help means a lot for me.
This script will do the trick:
var regex = /&pl=\d*/;
var match = regex.exec(yourtext)[0];
Following regex should do it
/&pl=\d*/.exec('http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5')[0]
Demo: Fiddel