How to remove set of string using regex [duplicate] - javascript

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 5 years ago.
On the following string I want to remove sort=*. I can get this done by run loop over it But want to get it done using regex which is new to me.
So start point should be "sort" and end point should be '&' and sort value could be *
I have created this one but dont how to add end point
string.replace((sort=*)\w,'')
"category_id=249&sort=name&category_filter=15&"
to
"category_id=249&category_filter=15&"

You ca use this regex instead (sort=.*?&), which can match every thing from sort= until the first occurrence of &
regex demo
Output
category_id=249&category_filter=15&

Related

Regex find url with specefic character inside (js) [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 1 year ago.
I have the following text:
Some text
{\"url\": \"https:\/\/www.someurl.com\/200x200\/fsdffsde3e", \"bbb\"} kljkl
{\"url\": \"https:\/\/www.someurl.com\/500x500\/fsdffsde3e", \"bbb\"} kljkl
Some text
I want to extract all the URLs contains 200x200. so for example I need this URL: https:\/\/www.someurl.com\/200x200\/fsdffsde3e
this, will bring the entire row: "https(.*?)200x200(.*)" but I want only the URL.
Have a look: https://regex101.com/r/DDWLDM/1
Thanks
use This exp
"https(.*?)200x200(.*?)"

Javascript string search double and single quote using RegExp [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
javascript includes() case insensitive
(11 answers)
Closed 2 years ago.
I'm using the following code to search sub string in a string
mystring.search(new RegExp(substring, 'i'))
Reason why I am using new RegExp is, I want to search case insensitive. However, when there is a string like
var mystring = '10" stick';
and I want to search 10", the code above does not return any result. It's clearly because of new RegExp and double quote. Is there any particular flag that needs to be passed in new RegExp? I googled a lot but couldn't find any solution. What am I missing?
search returns match position, maybe ur confused by this.
So, try this out,
const myString = '10" stick';
console.log(myString.match(new RegExp('10"', 'i'))[0])
console.log(myString.match(new RegExp('ick', 'i'))[0])
console.log(myString.match(new RegExp('asd"', 'i'))) // non-matching
It returns the match or null if theres non. AND it matches 10" in 10" stick
see String.prototype.search mdn

Get text between tags regex javascript [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I am trying to get the text between the two tags which also handles multiple lines.I have managed to do it in PHP regex but i am stuck on the javascript one.
<any_tag>random text
dffdffdfdfdfdfdfdfd
dfdfdfdfdfdfdfdfdf
</any_tag>
I want to get the text between <any_tag> and </any_tag>
This is the regex for php
https://regex101.com/r/tQ1bX7/2
Now i am trying to achieve to same in javascript
Give try on following :
/[^(<any_tag>)](?:.*(\r?\n?).*)*(?=\<\/any_tag\>)/gi
Explination :
[^(<any_tag>)] match a single character not present in the list below
(<any_tag>) a single character in the list (<any_tg>) literally (case insensitive)

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

Replace values between parentheses using Javascript and Regex [duplicate]

This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).

Categories