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}))
Related
This question already has answers here:
Match exact string
(3 answers)
Closed 2 years ago.
I am trying to find an solution to return a true value if certain expression B which follows expression A is valid.
For instance -
If I'm trying to match the strings with the regex - F[A-Z]{0,2}
F
FA
FB
FAA
FAAA
where F is expression A here and [A-Z]{0,2} is expression B here
It is matching FAAA which it shouldn't since I have mentioned an quantifier max limit to 2.
So the expected output is -
F
FA
FB
FAA
JSFiddle
You need to either use:
\bF[A-Z]{0,2}\b
or
^F[A-Z]{0,2}$
you get true currently because a match does occur. You need some sort of limitation on the matching.
F[A-Z]{0,2} says match an F then 0 to 2 uppercase alpha characters. Anything before or after that can still exist.
See https://regex101.com/r/KLKTS4/2/ for a demo.
Adding a $ to the end of your regex should fix it:
F[A-Z]{0,2}$
https://regex101.com/r/1ADic0/2
The $ sign will make sure that you are at the end of line. You can see a lot of regexps starting with ^ and ending with $ (or \A and \z for multiline). This pattern basically means: my regexp should match the whole string.
You need to use $ in the end to asserts position at the end of a line and also need to put ^ to asserts position at the start of a line, so that values like BFA, BF could also be ignored. So the update regex would be like:
^F[A-Z]{0,2}$
DEMO
This question already has answers here:
Regex that can match empty string is breaking the javascript regex engine
(2 answers)
Closed 3 years ago.
Here is what I got in the console of Chrome 78.
console.log('1111'.replace(/(^|[^2])/g, '$12'))
// output "21121212"
Why isn't it replacing the first 1 with 12?
I think what's happening is that after replacing a zero-width match, it increments the position in the input string by 1 before searching for the next match. Otherwise, it would get stuck in an infinite loop, continually matching and replacing the same zero-width string.
Since ^ matches a zero-width string at the beginning, it increments the position, skipping over the first character of the string before looking for the next match.
Method 1
My guess is that you're trying to write
(?<=^)|([^2])
yet, you'd want to check if lookarounds are supported or not.
Demo 1
Method 2
This method also has lookarounds,
(?<=^|[^2])
Demo 2
If you would provide some sample intputs and outputs, there might be some workarounds.
For example, maybe a positive lookahead might be an option to look into:
(?=^|[^2]|$)
Demo 3
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
RegEx Circuit
jex.im visualizes regular expressions:
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
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.
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.