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.
Related
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}))
This question already has answers here:
RegEx - Get All Characters After Last Slash in URL
(8 answers)
Closed 2 years ago.
I currently have a url path like so:
var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
I wish to delete/remove all the string up to the last / slash.
If I wanted to replace the last slash I'd get it like this:
console.log(str.replace(/\/(?=[^\/]*$)/, '#'));
But I would like to delete everything up to the last slash and I can't figure it out.
In the above example I'd get back
you-do-too
Any ideas?
var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
console.log(str.replace(/^.*\//, ''));
^ matches the beginning of the string.
.* matches anything.
\/ matches slash.
Since .* is greedy, this will match everything up to the last slash. We delete it by replacing with an empty string.
I know it's not beautiful, but you can do str.split('/').pop() to get the last part of the URL.
No un-humanly readable regex
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.