Regex allows spaces [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
For the following regex expression:
var regex = new RegExp("^(www\\.)?[0-9A-Za-z-\\.#:%_\+~#=]+(\\.[a-zA-Z]{2,})+(/.*)?(\\?.*)?");
I don't understand why the string "www.goo gle.com" passes the regex test. When I did this:
var regex = new RegExp("^(www\\.)?[0-9A-Za-z-\\.#:%_\+~#=]+(\\.[a-zA-Z]{2,})+(/.*)?(\\?.*)?$");
i.e. adding $ in the end of the regex string prevents the above string passing, which is what I would want.
I tried finding a "simulator" online to help me figure out how the regex is matching but couldn't find much help.

www.goo gle.com passes the test since, www. is matched by [0-9A-Za-z-\\.#:%_\+~#=]+ and
goo is matched by (\.[a-zA-Z]{2,})+. In contrast, (www\\.)?, and the last two groups are optional, so the regex is satisfied even if they are not matched, hence there's no need to further match gle.com.
By adding $, the regex no longer matches, since the space is not matched by any of the subexpressions.

Related

How does this regex code work (positive lookahead)? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 1 year ago.
I entered the following regex:
(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[##$%]).{8,20}
And give the following input string for pattern search:
A00123456789123456789gj673%
It gave me the matched output as:
A0012345678912345678
Can you explain me why it's giving me output when the order of characters entered in input string doesn't matched the order in which regex pattern is entered?
If possible, plz explain me how this works sequentially.
Your pattern is being partially matched. If you want to prevent it's matching, you can do it by adding ^ and $ to the begin and end of your pattern.
^: asserts position at start of a line
$: asserts position at the end of a line
The final pattern would be like:
(?=^[a-z]*[A-Z]*[0-9]*[##$%]*$)(?=(.{8,20}))

How to capture only first occurence in this regex? [duplicate]

This question already has answers here:
What is the meaning of the 'g' flag in regular expressions?
(10 answers)
Closed 4 years ago.
I have this string:
2+*+2+*+3+*+3+*
And this regex:
(\d{1,3}\+\*\+\d{1,3})
Problem is, it captures both matches (2++2 and 3++3), and I need it to capture only the first occurence.
How can I change my regex to match this?
Here's the regex101 url for anyone who wants to try:
https://regex101.com/r/7G2adU/1/
Thanks!!
If you change your regex to:
^(\d{1,3}\+\*\+\d{1,3})
then only the first match will be captured. The caret ^ is a start-of-string anchor; it matches the start of the input string so that only the first occurrence of the rest of the regex is captured.
There is another alternative. In your example https://regex101.com/r/7G2adU/1/ your regex looks like this:
/(\d{1,3}\+\*\+\d{1,3})/ig
The g is the global flag; it tells the engine not to stop after the first match has been found, but rather to continue until no more matches can be found. If you remove that flag, only the first match will be returned.

Regex is also preventing new line [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
I have some confusion in Regex so I need help.my question is I am using the following Regex to prevent string should not start with some character and should not contain angular bracket.this regex also preventing next line as well so can u help me to modify it according to my need.
^(?![#=+*-])(?!.*[<>]).*$
Thanks
working example-->https://regex101.com/r/5GZQl7/1
The problem with your regex is that . does not match line endings, so as soon as you put a new line in there, the regex does not match.
Ideally, we want it to match everything, including line endings. What syntax can match everything? One way to do this is to use complementing character sets. \s matches all the whitespace, \S matches all the non-whitespace, so [\s\S] will match everything!
Replace all your .s with [\s\S]!
Demo

JS Regex to match character outside nested braces [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need to write a regex to target commas that exist only outside a pair of brackets or braces.
Currently I have:
var regex = /,(?![^{]*})(?![^[]*])/g
When the target string is:
var str = '"a":[{"b":2,"c":["d"]}],"b":2' // OK: only second comma matches
the pattern correctly matches only the second comma.
When the target string is:
var str = '"a":[{"b":2,"c":{"d":9}}],"b":2' // OK: only second comma matches
the pattern also correctly matches only the second comma.
However, when the target string includes a array and object, the negative lookahead fails and the regex matches both commas.
var str = '"a":[{"b":2,"c":[{"d":9}]}],"b":2' // BAD: both commas match
This regex will work (see demo) with the mentioned example:
,(?!([^{]*{[^{]*})?[^{]*})(?!([^[]*\[[^[]*])?[^[]*])
But it's not a generic regex.
For each level of nested brackets you need to expand the regex. For example, to match also "a":[{"b":2,"c":[{"d":[{"e":9}]}]}],"b":2 you will need:
,(?!([^{]*{([^{]*{[^{]*})?[^{]*})?[^{]*})(?!([^[]*\[([^[]*\[[^[]*])?[^[]*])?[^[]*])
See second demo.
This is not a scalable solution, but it's the only one with regexes.
Just for curiosity.

regex extract last occurrence ignoring new line [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 7 years ago.
Considering a string like:
"#foo #foo#foo#foo #foo\n
foofoofoo\n
foo #bar"
I try for 2 days to extract the last #/# occurrence so here, the # before 'bar'. For now, i have something like this [##](?!.*[##]) which seems to work except when user insert new lines in there.
Can someone give me some tips please?
You can use this lookhahead regex:
/[##](?![\s\S]*[##])/
RegEx Demo
(?![\s\S]*[##]) is the negative lookahead that asserts there is no # or # ahead of current position in any line. [\s\S] matches any character including newline.

Categories