Looking for a right regEx (Javascript) - 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

Related

Rex match a string in JS

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

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);

Match the second word with no specific word behind

This is a follow-up of:
javascript regex - look behind alternative?
In my situation, I'm looking to only match the second word when there is no specific word that preceeds the term. As with the prior issue I need a solution that doesn't utilize the look behind technique.
I'm looking to exclude mentions such as the following:
patient has a history of pulmonary edema
Using the expression:
((?!pulmonary ).{10})\bedema
But given the following sentence:
Continuing dyspnea and lower-extremity edema
I would like the match to only return edema instead of extremity edema.
Please try this pattern:
(?!pulmonary).{10}\b(edema)\b
The demo is here.

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

Javascript regex to match a person's height

I need a javascript regex pattern to match a person's height to check if the input is valid. Here are some sample input:
5' 9"
6'
5'8"
Any ideas?
If you want to make sure that no one mucks around with it, you could limit it to sensible ranges, eg: 3' to 7'11''
/^(3-7)'(?:\s*(?:1[01]|0-9)(''|"))?$/
I always thought that the "inches" mark was a double quote ("), compared to VonC's answer where he put it as two single quotes (''), so this regex takes both into consideration.
Maybe something like:
^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$
see: here
Something like:
\d'(?:\s*\d+'')?
The second part refers to optional part of the heigth.
Remove the + if you want only one digit.
\b\d'(?:\s*\d+'')?\b
can also be used to detect that pattern within a text (avoid detecting 1234'45 as an heigth for... a person?!)
You can test that regexp here for javascript.
Ok. Thanks for all your input. Wow, that was fast, Big time.
Anyway, I've tested all your regex and it seems Ruben's answer passed all my test input. Thanks a lot for that mate.
So here's the one that I need:
^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$
^\d'\s?(\d{1,2}")?$
Tested here: http://www.regular-expressions.info/javascriptexample.html

Categories

Resources