Rex match a string in JS - javascript

I have few set of strings as mentioned below
/v4/users/1
/v4/users/1/vehicles/1
/v4/users
/v4/users?page=1
I would like to get users in all four cases as output using regex in Javascript
I tried below in https://www.regextester.com/
(?<=/v4/).*.(?=/[^/]*/)
It doesn't seem to come up right.
Any help on this would be appreciated.

You were close with the positive lookbehind. This works:
'/v4/users/1/vehicles/1'.match(/(?<=\/v4\/)[^\/\?]*/)
This matches users because after the lookbehind you match everything until just before the next slash.

/\/v4\/(\w+)/g
This will put users in a capture group. If you want you can make it a named group as well.
You can try it here:
https://regex101.com/r/0OOr0g/1

Related

Looking for a right regEx (Javascript)

I am looking for a right regex that would match "/anyword/" and perhaps "/ln/anyword/" in both:
http://localhost:3000/anyword
https://www.somedomain/ln/anyword
The "ln" in the second domain can be any two letter. (As languages).
(?:\w)(/.+) See https://regex101.com/r/3Ahynm/2
The capture group contains the desired text
What work as the above answer suggested:
/(?:\w)(\/.+)/i

Why would the replace with regex not work even though the regex does?

There may be a very simple answer to this, probably because of my familiarity (or possibly lack thereof) of the replace method and how it works with regex.
Let's say I have the following string: abcdefHellowxyz
I just want to strip the first six characters and the last four, to return Hello, using regex... Yes, I know there may be other ways, but I'm trying to explore the boundaries of what these methods are capable of doing...
Anyway, I've tinkered on http://regex101.com and got the following Regex worked out:
/^(.{6}).+(.{4})$/
Which seems to pass the string well and shows that abcdef is captured as group 1, and wxyz captured as group 2. But when I try to run the following:
"abcdefHellowxyz".replace(/^(.{6}).+(.{4})$/,"")
to replace those captured groups with "" I receive an empty string as my final output... Am I doing something wrong with this syntax? And if so, how does one correct it, keeping my original stance on wanting to use Regex in this manner...
Thanks so much everyone in advance...
The code below works well as you wish
"abcdefHellowxyz".replace(/^.{6}(.+).{4}$/,"$1")
I think that only use ()to capture the text you want, and in the second parameter of replace(), you can use $1 $2 ... to represent the group1 group2.
Also you can pass a function to the second parameter of replace,and transform the captured text to whatever you want in this function.
For more detail, as #Akxe recommend , you can find document on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace.
You are replacing any substring that matches /^(.{6}).+(.{4})$/, with this line of code:
"abcdefHellowxyz".replace(/^(.{6}).+(.{4})$/,"")
The regex matches the whole string "abcdefHellowxyz"; thus, the whole string is replaced. Instead, if you are strictly stripping by the lengths of the extraneous substrings, you could simply use substring or substr.
Edit
The answer you're probably looking for is capturing the middle token, instead of the outer ones:
var str = "abcdefHellowxyz";
var matches = str.match(/^.{6}(.+).{4}$/);
str = matches[1]; // index 0 is entire match
console.log(str);

Unable to determine the regex for a path format which could be stringA/stringB/StringX or stringA/stringX but not just stringA/stringB

This regex is in JavaScript. More specifically stringA = content, stringB = dam & stringx could be any string.
I have tried this regex & few others:
^\/(content(?!\/(dam)))\/(.*)
but this would recognize
/content/asfcew
/content/reddam
/content/usa/texas
and would not recognize
/content/dam
which is good, but alongside it also does not recognize
/content/dam/asdfafa
/content/damred
which is not good.
Any suggestions are much appreciated, thanks.
You just need to add an end-of-string anchor $ to the look-ahead:
^\/(content(?!\/(dam$)))\/(.*)
^
See demo
Now, (?!\/(dam$)) will only fail the match when dam appears before the end of string.
Note that there are too many capturing groups here, you may remove them like this:
^\/content(?!\/dam$)\/(.*)
See another demo
As the poster above said, you need an end of string anchor $ to the look ahead group.
To enable it capture both /content/dam and the rest use this pattern.
> ^\/(content(?=|\/(dam$)))\/(.*)
See demo here https://regex101.com/r/kO2cZ1/5

Regular Expression in Javascript for finding parameters

I'm trying to write a regular expression in javascript to catch all named parameters in PostgreSQL string to put them in table
lets say we have
var query="SELECT table.data FROM table JOIN table2 ON table2.id=table.id_tab2 WHERE table2.field <> :parm1::int GROUP BY table.data HAVING table.data position(:docType::text in document_type) <> 0
var tab=new Array();
//
I need rs to put into tab all parameters: "param1::int" and "docType::text"
I tried do it myself but with no success :(
http://regexr.com?31nok
something like this? :(\w+::\w+)
and (:[a-zA-Z0-9]+::[a-zA-Z0-9]+)
There can be weird things between a-Z, so just use [a-z] with case insensitive or [a-zA-Z]. I think you probably want a global match (find all results). Multi-line is something else, it makes . match \n which doesn't help you that I can see. Try this: http://regexr.com?31not
I don't know postgreSQL, but I think I got the gist of your question.
Is this correct: http://refiddle.com/2tc?
when i try :([^: ]+::[^: ]+) and (:[^: ]+::[^: ]+) i see no difference- colon is in match in both casec.
However i found some other way
(?!:)([a-z0-9_-]+::[a-z0-9_-]+)
works perfectly.
first part (?<=:) determine "Matches a group before your main expression without including it in the result."
Thanks you all for your answers ;)

need to modify this Regex to not remove a character

I have the following regexp (/\?(.*?)\&/) which when I use it in the following javascript code it removes the "?" from the replacement result.
href=href.replace((/\?(.*?)\&/),"")
The beginning href value is this...
/ShoppingCart.asp?ProductCode=238HOSE&CouponCode=test
I get this as my result right now...
/ShoppingCart.aspCouponCode=test
I would like to get this...
/ShoppingCart.asp?CouponCode=test
How would I modify the Regexp to do this
Thanks for you help.
Put a question mark in the replacement substring:
href=href.replace((/\?(.*?)\&/),"?")
If, say, the character can be something else than a question mark as well (say maybe a slash is a possibility), and you need to preserve which one it is, you can use a capturing group:
href=href.replace((/([?\/])(.*?)\&/),"$1")
Lookbehinds are not supported in JavaScript regexes.
To do it properly, you'll need a regex lookbehind, however this should work in your case:
href=href.replace((/\?(.*?)\&/),"?")

Categories

Resources