Regex - find specific pattern not surrounded by [] [duplicate] - javascript

This question already has answers here:
How to search regex only outside curly brackets
(2 answers)
Closed 8 years ago.
In given random string: '#id1 .class1 .1class [price="23 .23 "] .7ytr"
I want to replace the pattern - dots followed by number followed by {n} alpha numeric like: .8acb8
But ignore it when it's surrounded by "[]" like - [price="23 .23 "].
For now I wrote only the pattern I need to find without ignoring []:
str.replace(/\.(\d+\w+)/g, '[class="$1"]');

You're going to use negative lookaheads:
\.\d\w+(?![^[]*\])
Explanations:
\.\d\w+ # Any dot followed by a number followed by alpha-numeric characters
(?![^[]*\]) # Which is not inside brackets (Negative Lookahead)
Live demo

\[[^]]*\]|(\.\d\w+)
Try this.Grab the captures or matches.See demo.
http://regex101.com/r/qU4wM7/1

Related

What is the regex that allows alphanumeric and '-' special character that also should be in between the text not at the begining or ending [duplicate]

This question already has answers here:
Regex to match '-' delimited alphanumeric words
(5 answers)
Closed 8 months ago.
All the special character except hyphen are not allowed.
Other conditions:
-xnnw729 //not allowed
nsj28w- // not allowed
aks82-z2s0j // allowed
Some notes about your answer:
Using \w also matches \d and _
For a match only you don't need all the capture groups
If you want to validate the whole line, you can append $ to assert the end of the line
Using a plus sign in the character class [\w+\d+_] matches a + character and is the same as [\w+]
You can simplify your pattern to:
^\w+(?:-\w+)*$
Regex demo
The one I was looking for is
^([\w+\d+_]+)((-)([\w+\d+_]+))*

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 not allow empty spaces at the end [duplicate]

This question already has answers here:
RegEx for no whitespace at the beginning and end
(18 answers)
Closed 2 years ago.
My target is to improve my regex. Regex need to everything except some special charaters, not to allow empty spaces at start, and not to allow empty spaces at the end.
^(?!\s*$)[^-\s][^`=~!##$%^&*()[\]\/\\{}"|<>?]{3,100}$
Example:
word valid
word [space] invalid
[space] word invalid
word w valid
My regex did everything except empty space at the end. How to add this condition to forbit empty spaces at the end of regex?
You may add another negative lookahead to disallow space at the end:
^(?!\s*$)(?![^]*\s$)(?![-\s])[^`=~!##$%^&*()[\]\/\\{}"|<>?]{3,100}$
(?![^]*\s$) is negative lookahead to assert that your regex won't allow a space at the end.
RegEx Demo

Use regex backreference inside character except class [^] [duplicate]

This question already has answers here:
Negating a backreference in Regular Expressions
(6 answers)
Closed 4 years ago.
Lets take:
stringi = 'xnxx xnnx xnnxn'
My regex is: (n)[^n]
I want to make my regex a little more dynamic like that:
(n)[^\1] -\1 beeing the capt. grp 1
My desired result would be that:
(n)[^\1] would be equal (n)[^n]
(x)[^\1] would be equal (x)[^x]
How can I not match a NOT-\1 character?
using a negative lookahead, the . is to match any character as n length is one
(n)(?!\1).

Javascript Regex for custom validation [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I need regex
1) [A-Z][a-z][0-9] Must include uppercase & lowercase letters, numbers & special characters (except + and -).
2) Not more than 2 identical characters in a sequence (e.g., AAxx1224!# or Password#123 or Google#12 is not acceptable).
I have tried this but dont know how to check 2 identical characters.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^%*()!&=]).*$
You may add an additional (?!.*(.)\1) lookahead check to disallow consecutive characters and replace .* at the end with [^_+]* (or [^-+]* if you meant hyphens) to match any chars but _ (or -) and +:
^(?!.*(.)\1)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^*()!&=])[^_+]*$
^^^^^^^^^^ ^^^^^
The (?!.*(.)\1) lookahead matches any 0+ chars other than line breaks chars and then captures these chars one by one and tries to match the identical char immediately after them (with the \1 backreference). If the pattern is found, the whole match is failed.
Note that [^_+] may also match line breaks, but I guess it is not the problem here. Anyway, you can add \n\r there to avoid matching them, too.
See the regex demo

Categories