Specific Regular Expression In Javascript with a condition [duplicate] - javascript

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

Related

how to generate regex in javascript? [duplicate]

This question already has answers here:
How to parse a URL?
(6 answers)
Closed 8 months ago.
This is the format for which I want to generate regex https://<any>.blob.core.windows.net/<any>?<any> where any allowed any character(including special char also). I mean all where the other part is static.
Can you help to generate regex for above pattern?
Thanks
This will match anything in the places where you put <any>.
https:\/\/.*\.blob\.core\.windows\.net\/.*\?.*
/^https:\/\/.+\.blob\.core\.windows\.net\/.+\?.+$/gm
<any> - any character (except for line terminators) between one and unlimited times. Can't be empty

Regex for replacing a particular occurrence of a String in Javascript [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
For some weird reason, I can't seem to figure out how to solve my current issue.
I have a string that contains this string /partners: and I wish to remove all occurrences of /partners: from the string
I have tried the below code
retrievedData = retrievedData
.replace("/\/partners:", "")
But for some weird reason it is not working.
Any help would be greatly appreciated.

RegExp with variable in a string format [duplicate]

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.

Replacing a special character in a string [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
I have a simple string like this, let string = "123½"; and I want to replace it.
I've already tried string.replace("½","0.5")
I'm not exactly what is happening here, but maybe it's because it's a special character?
string.replace doesn't modify a string; it returns a new string. Try:
const replaced = string.replace("½","0.5");

getting a number using regular expressions [duplicate]

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

Categories