how to use string searching syntax in javascript - javascript

Can someone please explain the syntax of searching through strings? For example, I have this piece of code:
var ok = phone.value.search(/^\d{3}-\d{4}$/);
phone is a variable that is supposed to contain a phone number, and I know from context that this is supposed to make sure the inputted number has the format ###-####, but I don't know what the code within the parenthesis means or how it is evaluated. If someone has a link explaining how to use code like that I would especially appreciate it.

That's a regular expression ( regex ),
Regex One has a good guide on how to use them
Your regex says "beginning with 3 digits, then a "-" then 4 digits"

It's a regular expression, a whole world in itself.
http://www.regular-expressions.info/tutorial.html

It is regex object. The ^ matches the beggining of the string, the \d{3} matches 3 digits, the - matches a dash, the \d{4} matches for digits, and finally the $ matches the end of the string.

What you have there is called a "regular expression" and as you say, they are used to ensure input matches a certain pattern. I recommend you go somewhere like http://www.regular-expressions.info/ for further info rather than re-post data here.

Related

Regex finding the last string that doesnt contain a number

Usually in my system i have the following string:
http://localhost/api/module
to find out the last part of the string (which is my route) ive been using the following:
/[^\/]+$/g
However there may be cases where my string looks abit different such as:
http://localhost/api/module/123
Using the above regex it would then return 123. When my String looks like this i know that the last part will always be a number. So my question is how do i make sure that i can always find the last string that does not contain a number?
This is what i came up with which really stricty matches only module for the following lines:
http://localhost/api/module
http://localhost/api/module/123
http://localhost/api/module/123a
http://localhost/api/module/a123
http://localhost/api/module/a123a
http://localhost/api/module/1a3
(?!\w*\d\w*)[^\/][a-zA-Z]+(?=\/\w*\d+\w*|$)
Explanation
I basically just extended your expression with negative lookahead and lookbehind which basically matches your expression given both of the following conditions is true:
(?!\w*\d\w*) May contain letters, but no digits
[a-zA-Z]+ Really, truly only consists of one or more letters (was needed)
(?=\/\d+|$)The match is either followed by a slash, followed by digits or the end of the line
See this in action in my sample at Regex101.
partYouWant = urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
Here it is in action:
urlString="http://localhost/api/module/123"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
urlString="http://localhost/api/module"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
It just uses a capture expression to find the last non-numeric part.
It's going to do this too, not sure if this is what you want:
urlString="http://localhost/api/module/123/456"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
/([0-9])\w+/g
That would select the numbers. You could use it remove that part from the url. What language are you using it for ?

Unable to find a string matching a regex pattern

While trying to submit a form a javascript regex validation always proves to be false for a string.
Regex:- ^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
I have tried following strings against it
abc.jpg,
abc:.jpg,
a:.jpg,
a:asdas.jpg,
What string could possible match this regex ?
This regex won't match against anything because of that $? in the middle of the string.
Apparently using the optional modifier ? on the end string symbol $ is not correct (if you paste it on https://regex101.com/ it will give you an error indeed). If the javascript parser ignores the error and keeps the regex as it is this still means you are going to match an end string in the middle of a string which is supposed to continue.
Unescaped it was supposed to match a \$ (dollar symbol) but as it is written it won't work.
If you want your string to be accepted at any cost you can probably use Firebug or a similar developer tool and edit the string inside the javascript code (this, assuming there's no server side check too and assuming it's not wrong aswell). If you ignore the $? then a matching string will be \\\\w\\\\ww.jpg (but since the . is unescaped even \\\\w\\\\ww%jpg is a match)
Of course, I wrote this answer assuming the escaping is indeed the one you showed in the question. If you need to find a matching pattern for the correctly escaped one ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(\.jpeg|\.JPEG|\.jpg|\.JPG)$ then you can use this tool to find one http://fent.github.io/randexp.js/ (though it will find weird matches). A matching pattern is c:\zz.jpg
If you are just looking for a regular expression to match what you got there, go ahead and test this out:
(\w+:?\w*\.[jpe?gJPE?G]+,)
That should match exactly what you are looking for. Remove the optional comma at the end if you feel like it, of course.
If you remove escape level, the actual regex is
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
After ^start the first pipe (([a-zA-Z]:)|(\\{2}\w+)\$?) which matches an alpha followed by a colon or two backslashes followed by one or more word characters, followed by an optional literal $. There is some needless parenthesis used inside.
The second part (\\(\w[\w].*))+ matches a backslash, followed by two word characters \w[\w] which looks weird because it's equivalent to \w\w (don't need a character class for second \w). Followed by any amount of any character. This whole thing one or more times.
In the last part (.jpeg|.JPEG|.jpg|.JPG) one probably forgot to escape the dot for matching a literal. \. should be used. This part can be reduced to \.(JPE?G|jpe?g).
It would match something like
A:\12anything.JPEG
\\1$\anything.jpg
Play with it at regex101. A better readable could be
^([a-zA-Z]:|\\{2}\w+\$?)(\\\w{2}.*)+\.(jpe?g|JPE?G)$
Also read the explanation on regex101 to understand any pattern, it's helpful!

Regular Expression Telephone Number Validation

I am trying to validate Phone Numbers that follow the next Pattern
01\d{9}
2\d{7}
But The user can enter more than one number separated by space or in one input field
So I come up with the regular expression
/(?:(?:01\d{9}$)|(?:2\d{7}$) ){1,}
A Test Sample
"01226113130 26322612 24586154 01004598654"
My Expression Doesn't match this sample, any help ?
Solution
For others if they fail in the problem, You can try Jerry Solution or this one
(?:(?:(?:01\d{9}(?:[\- \,])*)|(?:2\d{7}[\- \,]*))){1,}
Try this one:
^(?:(?:01\d{9}|2\d{7}) ){1,}(?:01\d{9}|2\d{7})$
Your current regex has (?:01\d{9}$)|(?:2\d{7}$) where the $ forced it to 'prematurely end' the match, so removing this was the first thing to do. Then (?:01\d{9})|(?:2\d{7}) can be re-written as (?:01\d{9}|2\d{7}). I added a ^ for the beginning of the string.
Afterwards, this regex will only validate strings ending with a space, so add another (?:01\d{9}|2\d{7}) at the end and finally conclude with $.
regex101 demo.
Oh, also, it might be better to turn the {1,} into * like this:
^(?:(?:01\d{9}|2\d{7}) )*(?:01\d{9}|2\d{7})$

Regular expression for blood pressure

I have the following regular expression to validate blood pressure values in the form of systolic/diastolic:
\b[0-9]{1,3}\/[0-9]{1,3}\b
The expression works with the only flaw that it allows more than one non-consecutive slash (/). For example, it allows this 2/2/2. I want it to allow only the format of a number from 1 to 999, and slash, and again a number from 1 to 999. For example, 83/23, 1/123, 999/999, 110/80, etc. Can anybody give me some help with this?
The only other expression I've found is here: ^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$, but it doesn't work.
Use ^ and $ to match the beginning and end of the string:
^\d{1,3}\/\d{1,3}$
By doing so, you force the matched strings to be exactly of that form.
Don't use the \b word-boundaries because a slash counts as a word boundary.
The use of ^ and/or $ is likely your most simple solution. Unfortunately, if your input is a part of a string or sentence or occurs more than once in a line, etc., you've got more thinking to do.
Expanding on Blender's answer, here is a simple check for validating BP value in the format: 120/80:
if(/^\d{1,3}\/\d{1,3}$/.test(120/80)) {
console.log("BP Valid");
} else {
console.log("Invalid BP");
}
^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$
This is correct, the other had one extra \

Regular expression that matches a pattern but excludes a match when it starts with a certain character

I have a string of the format:
PATTERN or abcPATTERNdef or PATTERN or <<[TEST].[PATTERN]>>
I have created a regular expression (in JavaScript) as (^PATTERN)|([^\[]PATTERN) which is returning the first 3 occurrences while ignoring the last one, however I also seem to be getting the preceding character in the returned matches:
"PATTERN", "cPATTERN" and " PATTERN"
What I need are the matches without the preceding character.
I'm new to regular expressions and apologize if the question reflects that.
Any help would be greatly appreciated.
JavaScript doesn't have good support for lookbehinds, but if your format is going to remain the same, you can try something like this:
PATTERN(?!])
It matches all occurrences of "PATTERN" that are not followed by a ].
Let me know if this isn't the case, and I will update my answer to include the check for the opening [.
I'm a little confused as to what you're trying to do. Can you see if this works?
(^PATTERN)|[^\[](PATTERN)
Are you trying to find all the strings between the PATTERN's?

Categories

Resources