Javascript regex to match a person's height - javascript

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

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

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

Js regex needed

I have a big problem trying to write some regular expressions for some string comparison. I have trying for over 2 hours now and lost my patience.
In short what I try to do get the letters after the cgid and before the end-of-line or next & in the following strings:
#cgid=neu and #cgid=neu&start=2
I have tried using stringVar.match("/&cgid=(.*?)&/") but it returns null.
I have also to get the last word after / in the following string:
"/s/SiteName/neu".
I know this is easy but it seems I am too hard headed to learn regex.
Why would you show strings with #cgid=... and then try to match it with &cgid=...? That makes no sense at all. Regexes are perfectly logical, by definition of the term "REGULAR expression". It's easy:
stringVar.match(/\bcgid=([^&]+)/);
Your desired target will be in the [1] index of the array.
For your "last word after /" part, try this:
stringVar.split("/").pop();
\w+=([^&]+)
You have that in first capturing griup here
Try group 1 from:
cgid=(.*?)(&.*)?

How to modify regex for phone numbers and accept only digits

I have this following regex method for the jquery validate plugin.
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");
Currently, its validating against phone numbers in this format : 203-123-1234
I need to change to validate like this: 2031231234
Does anyone have a quick and easy solution for me?
You can replace
phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
with this
phone_number.match(/\d{10}/);
\d means match any digit
and
{10} means 10 times
Getting rid of all those -? sequences is probably the quickest way - they mean zero or one - characters.
That will reduce it to:
/^(1)?(\([2-9]\d{2}\)|[2-9]\d{2})[2-9]\d{2}\d{4}$/
whih can be further simplified to:
/^1?(\([2-9]\d{2}\)|[2-9]\d{2})[2-9]\d{6}$/
If you also want to disallow the brackets around area codes, you can further simplify it to:
/^1?[2-9]\d{2}[2-9]\d{6}$/
(and, technically, it won't match the literal 203-123-1234 since the character immediately after that first - has to be 2 thru 9, so I'm assuming you were just talking about the format rather than the values there).
I think better approach would be changing the whole expression with simpler version, something like this:
/^[0-9]{10}$/
Edited, Note (see comments):
This is just a limited example of how to validate a format: 111-222-3333 vs 1112223333, not proper US phone number validation.
If you just want ten digits, then
phone_number.match(/\d{10}/)
will do it. If you want to match any of the other conditions in there (eg match both 1-2031231234 and 2031231234), you will need to add more.
As a side note, what you currently have doesn't match 203-123-1234 because the first digit after the first hyphen is a 1, and it is looking for 2-9 in that spot.
([0-9]{10}) this will match with 10 digit number.
You can use if you want to match all formats, including 203-123-1234 and 2031231234
EDIT : I'm no regex expert, but I added "1-" support
/^(?:1-?)?[(]?\d{3}[)]?\s?-?\s?\d{3}\s?-?\s?\d{4}$/
By the way, there's a really nice AIR tool for regex, it's called RegExr and you can get the desktop version here http://www.gskinner.com/RegExr/desktop/ or use the online version http://gskinner.com/RegExr/ . There's also a "community" section that contains a lot of useful working regex. That's where I took that one.

Regex in JavaScript

Suppose we don't know how many slashes we could get in a string but we do not want any extra slashes. So if we get this string '/hello/world///////how/are/you//////////////' we should transform it to the form of '/hello/world/how/are/you/'. How to do it with the help of regular expressions in JavaScript?
"/hello/world///////how/are/you//////////////".replace(/\/+/g, "/")
'/hello/world///////how/are/you//////////////'.replace(/\/{2,}/g, '/');
This might be an incy wincy bit faster than mkoryak's suggestion, for it will only replace where necessary – i.e., where there's multiple instances of /. I'm sure someone with a better understanding of the nuts and bolts of the JavaScript regular expression engine can weigh in on this one.
UPDATE: I have now profiled mine and mkoryak's solutions using the above string but duplicated hundreds of times, and I can confirm that my solution consistently worked out several milliseconds faster.
Edited: mkoryak's answer below is way better. Leaving this in case the info it contains is useful for someone else.
You could capture each word + slash group and look ahead (but don't capture) for one or more extra slash. Like...
(\w+\/)(?:\/)*(\w+\/)(?:\/)*
First () captures one or more of any word character followed by a slash, second () looks for a slash but doesn't capture, * means find 0 or more of the proceeding token. Etc.
Hope that helps!
I want to make a regex for string which matches from point A till point B
text= "testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh Executed tsttt\n fhgkhkh"
Output should be
testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh
I want to make a regex for string which matches from point A till point B
text= "testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh Executed tsttt\n fhgkhkh"
Output should be
testttt<font color='red'>Executed 'show bootvar' on \n</font>10.238.196.66. kjdkhfkh <font color='red'>Executed tsttt\n</font> fhgkhkh

Categories

Resources