Confusing regex. I can't understand this part: +$/ [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I was doing a hackerrank here and I did the following code:
/^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)\w/
But the answer was
/^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)\w+$/
and I don't understand the last part of that regex. What is it?

Here, \w will select An alphanumeric character (“word character”), and When you put a plus sign (+) after something in a regular expression, it indicates that the element may be repeated more than once.
Thus, /\w+/ matches one or more alphanumeric characters.
And $ here means End of string.
Example 1 --- /^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)\w$/.test('Mr.J'); // true
Example 2 --- /^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)\w$/.test('Mr.Joseph'); // false
Example 3 --- /^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)\w+$/.test('Mr.Joseph'); // true

Related

How to check the sting is alphanumeric or not in peggyjs [duplicate]

This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 1 year ago.
I want to check the given string is alphanumeric or not. i.e. the expected output is as follows
123 should retun false
abc should retun false
a123 should retun true
1a23 should retun true
I tried with the ^[a-zA-Z0-9]*$ regex. It is not working as expected. Can anyone suggest the working peggyjs regex? Thanks.
You can assert not only digits, and match at least a single digit restricticting to match to only a-z or a digit.
Using a case insensitive match:
^(?!\d+$)[a-z]*\d[a-z\d]*$
Regex demo
If you know the order (letters then numbers for example) you can do .*[a-zA-Z].*[0-9]
But I assume you can't make such assumptions so I would use the slightly more complex ^(?=.*[a-zA-Z])(?=.*[0-9]).* which means "a letter somewhere later, and also a number somewhere later".
PS : you can replace all [0-9] by \d if you like.
Edit : that's only assuming you don't get other kinds of characters, use Alireza's regex instead if you need to.

How to use end of string in square brackets in javascript regex? [duplicate]

This question already has answers here:
Using $ anchor inside a character class does not work
(2 answers)
Closed 3 years ago.
In js regex, I have
[\.\?!][\s$]
what I want to do is match
literal dot, or literal question mark or explanation mark
then
either 1 whitespace character or, be at the end of the string.
However the regex above, tries to match the literal $.
Does anyone know how to fix this?
Thanks
Try this Regex:
[.?!](?:\s|$)
Click for Demo
Explanation:
[.?!] - matches either a . or a ? or a ! literally
(?:\s|$) - matches either a white-space or the End-of-line

Word boundary not working for amount in dollars (REgex) [duplicate]

This question already has answers here:
how to reduce complexity in regex?
(3 answers)
Closed 5 years ago.
I have a regex which will match amount in $.
(\-?\$\-?[0-9]+\.?[0-9]*|usd\-?[0-9]+\.?[0-9]*|[0-9]+\.?[0-9]*\-?usd|[0-9]*\.?[0-9]*\$)
Currently its matching for $250, USD250 etc, it should not match the $250 in $250abchhh.
So, I tried word boundary, but it didn't fix the issue as well, how can I fix this issue?
matching cases are
$456
$45.6
$.5
$-45
-$45
usd-456
usd46
usd4.6
usd.46
1$
1.5$
.5$
-.5$
5usd
456usd
it should not match
455$abc
abc$123
abcuds1
jhb$5665usdjnjnb
$usd1555
usd$768
$566usd
$5788usdbjhj
You should also write a correct regex for a decimal. And use ^$ for start and end of line.
^\s*(?:(?:(?:-?(?:usd|\$)|(?:usd|\$)-)(?:(?:0|[1-9]\d*)?(?:\.\d+)?(?<=\d)))|(?:-?(?:(?:0|[1-9]\d*)?(?:\.\d+)?(?<=\d))(?:usd|\$)))\s*$
Look here at the test results.

Regex query, description [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I have a regex query for password verification, the rules are password must be between 8-15 chars, 1number + 1 special characters. It is working perfectly in web form.
I only need to understand it fully. If anyone can help me in describing this regex group by group,, it will be of great help to me. I do understand some part but not all.
^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{7,15}$
Since you updated the regex...
^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{7,15}$
^(?=.*[0-9]) from the start of the string, match any numbers. The lookahead ?= prevents the regex from continuing if nothing matches.
(?=.*[!##$%^&*]) match any special characters in the group.
[a-zA-Z0-9!##$%^&*] capture all letters, numbers, and special characters. At least 7 and up to 15 until the end of the line.

Regex pattern to match this string [duplicate]

This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group

Categories

Resources