regex extract last occurrence ignoring new line [duplicate] - javascript

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.

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}))

javascript regex must include 1 dot in the middle but no dots in beginning or end [duplicate]

This question already has an answer here:
RegEx only one dot inside string not at beginning or end
(1 answer)
Closed 2 years ago.
this is what I've been able to find so far /^[^.].*[^-_.]$/ I can't get the required middle dot to work, however. I tried adding [\.*] to the middle but that doesn't work.
At the beginning, repeat a non-dot one or more times with +, then match a literal dot with \., then match anything with .* up until the final character:
^[^.]+\..*[^-_.]$

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, $ end of input not able to use [duplicate]

This question already has answers here:
Validate phone number with JavaScript
(30 answers)
Closed 5 years ago.
console.log(/\d+?\d+?\d+?-\d+?\d+?\d+?-\d+?\d+?\d+?\d+?$/.test("555-555-55539"));
Answer --> true
I was looking for false, i am validating phone numbers. e.g. 555-555-5555 is a correct response([0-9])
I am a newbie to regex, can anyone explain what i am doing wrong here?
How about this.
console.log(/\d{3}-\d{3}-\d{4}$/.test("555-555-55539"));
You used wrong quantifiers in your regex. You made them lazy (+?), but it will still match all characters until the next character from regex is found. In case of your last quantifier (just before $) it will match all digits until the end of string is found. Hence it matches not only one digit but all of them. Same thing happens before each hyphen (555555555-5555-555555555 is valid for your regex).

Regex allows spaces [duplicate]

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.

Categories