This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 5 years ago.
I need a RegEx that select something like [something and not support [something] for first one I know this
/\[[\w]+/g
But for not selecting if content between [] I don't know what should I do.
Regex can't necessarily solve your problem.
If you're always matching the entire string, you can ensure that the end of the string comes before any occurrence of the "]" character:
var reg = /\[[^\]]+$/;
/*
How is this regex working?
- The first two characters, `"\["`, mean to match a literal "["
- The next 5 characters, `"[^\]]"`, match ANY character except
the "]" character. The outer "[]" define a character class,
and when "^" appears as the first character of a character
class it means to invert the character class, so only accept
characters which DON'T match. Then the only character which
cannot be matched is an escaped right-square-bracket: "\]"
- Add one more character to the previous 5 - `"[^\]]+"` - and
you will match any number (one or more) of characters which
aren't the right-square-bracket.
- Finally, match the `"$"` character, which means "end of input".
This means that no "]" character can be matched before the input
ends.
*/
[
'[',
'[aaa',
'aa[bb',
'[[[[',
']]]]',
'[aaa]',
'[aaa[]',
'][aaa'
].forEach(function(val) {
console.log('Match "' + val + '"? ' + (reg.test(val) ? 'Yes.' : 'No.'));
});
Related
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+_]+))*
This question already has an answer here:
Parenthesis not being replaced in regex
(1 answer)
Closed 4 years ago.
I'd like to remove all occurences of "(" and ")" in a string but the following replace line is throwing up a 'group not terminated' error.
str = "1+((x*(2*3))+10)";
console.log(str.replace(//(/gi,"");
How should I do this?
A '(' character has special meaning in RegEx (start of Group), you must escape the parentese like this:
\(
The same for an end parentese. Alternatively you can use a character Group like this:
[()]+
That will select any character in the Group (in this case parenteses) on or more times.
Try replacing a character class containing both opening and closing parentheses:
var str = "1+((x*(2*3))+10)";
console.log(str.replace(/[()]+/gi,""));
1+x*2*3+10
But, it is not clear why you would want to do this, because most likely removing all parentheses would change the value of the expression.
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
This question already has answers here:
Regex - Should hyphens be escaped? [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to understand dash character - needs to escape using backslash in regex?
Consider this:
var url = '/user/1234-username';
var pattern = /\/(\d+)\-/;
var match = pattern.exec(url);
var id = match[1]; // 1234
As you see in the above regex, I'm trying to extract the number of id from the url. Also I escaped - character in my regex using backslash \. But when I remove that backslash, still all fine ....! In other word, both of these are fine:
/\/(\d+)\-/
/\/(\d+)-/
Now I want to know, which one is correct (standard)? Do I need to escape dash character in regex?
You only need to escape the dash character if it could otherwise be interpreted as a range indicator (which can be the case inside a character class).
/-/ # matches "-"
/[a-z]/ # matches any letter in the range between ASCII a and ASCII z
/[a\-z]/ # matches "a", "-" or "z"
/[a-]/ # matches "a" or "-"
/[-z]/ # matches "-" or "z"
- may have a meaning only inside a character class [], so when you're outside of it you don't need to escape -
I need to replace old value between foo{ and }bar using Javascript regex.
foo{old}bar
This works if old is a single line:
replace(
/(foo{).*(}bar)/,
'$1' + 'new' + '$2'
)
I need to make it work with:
foo{old value
which takes more
than one line}bar
How should I change my regex?
Change your regex to,
/(foo{)[^{}]*(}bar)/
OR
/(foo{)[\s\S]*?(}bar)/
so that it would match also a newline character. [^{}]* matches any character but not of { or }, zero or more times. [\s\S]*? matches any space or non-space characters, zero or more times non-greedily.