This question already has answers here:
How do you match a caret (^) symbol in regex?
(2 answers)
Closed 5 years ago.
Will the match method in Javascript find a ^ caret character?
This is not working for me.
var theString = '^A^B^C^D';
var theMatch = theString.match(/^/g);
The ASCII code for the caret is 94. Can I match it by the ASCII code?
^ is a special character. You must escape it:
var theMatch = theString.match(/\^/g);
To complement #syntax excellent response. Please note that some characters are like "reserved keywords" in Regular expressions and any time you need to use them you will have to use \ followed by the character some other examples are \. \$ \[ \( and many others.
If need some additional help with regular expression I would like to recommend you a site that does an excellent job reading at your regular expression and this can help you understand them better:
http://regex101.com/
Related
This question already has an answer here:
Allow alphanumeric with spanish regex in javascript?
(1 answer)
Closed 1 year ago.
I have the following regex set up to accept words and some special characters:
const regex = /^[\w\-'.,?\/()\[\]!&\s]+$/;
I want to extend this to also include the range of special characters in Spanish: ñáéíóú
I found this answer which provides a regex for all special chars, but I'm not sure how to incorporate this kind of solution into my already existing regex.
You can simply add those characters to the class you already have in your regex:
const regex = /^[\wñáéíóú\-'.,?\/()\[\]!&\s]+$/;
It is not needed to add the u modifier.
NB: it is not really necessary to escape the [ character inside a character class.
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 answers here:
javascript regex - look behind alternative?
(8 answers)
Closed 6 years ago.
I'm converting a python script I wrote to javascript. My python script has the following regex to match single instances of '\':
re.sub(r'(?<!\\)(\\{1})(?!\\)', r'\\', word)
I got a compiler error when trying to run this in js:
"Invalid regular expression: /(?<!\\)(\\{1})(?!\\)/: Invalid group"
After some searching found out that regex in js does not support look behinds.
I looked at this answer and they used:
^(?!filename).+\.js
in the form of a negative look-ahead from the start of the string, which does not help me as I need to change '\' to '\\' anywhere in the string.
I do not think this is a duplicate question as my question is trying to determine how to avoid and match the same character at different points in a string, while the linked question seeks to avoid a specific phrase from being matched.
I need to match '\' characters that do not have '\' either before or after them.
You always can use capture groups instead of lookbehind
string.match(/(^|[^\\])(\\{1})(?!\\)/)[2]
let replaced = "a\\b\\\\".replace(/(^|[^\\])(\\{1})(?!\\)/, x => x[0] == '\\' ? x : 'value')
console.log(replaced)
will return you same thing as (?<!\\)(\\{1})(?!\\)
Just match without assertions (^|[^\\])\\([^\\]|$) then substitute them back.
Note that this will tell you nothing about if it is escaping anything or not.
That regex is more complex.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
Can someone elaborate the following regular expression:
/^[\w0-9.-]{1,}#[A-z0-9]{1,}.[A-z]{3}$/
and also give some sample strings that satisfy this regular expression?
Thanks
Looks like a crude regex to check for an email address. Not the proper complete one, mind you (it's a lot longer).
^ beginning of string
[\w0-9.-] - word character, a digit, a dot or a dash. Doesn't make that much sense as word characters include digits too, so it can be simplified to [\w.-]
{1,} - one or more of those. There is an equivalent +, it's better to use that instead
# - at sign
[A-z0-9] - a terrible idea to mix capital and lower case letters. As it is right now, this means all ascii characters from A to z plus digits
. - any character. I'm guessing it should have been a literal dot - \.
[A-z]{3} - three characters, again as above
$ - end of line
So my guess is that this was a poor's man attempt at email validation. Here is the simplified version with the [A-z] shenanigan fixed:
/^[\w.-]+#[A-Za-z0-9]+\.[A-Za-z]{3}$/
See it in action
As for something which satisfies the original regex - .#A.AAA
You should checkout http://regexper.com which illustrates regular expressions (Note, I fixed the escaping of the period for you):
From the illustration you can see it is checking for:
The start of the string
One or more characters of: a word character, period or dash
Followed by a single "#" symbol
Followed by one or more characters within the ranges of A-z or 0-9
Followed by a period
Followed by three characters within the range of A-z
The end of the string
as #Kayaman mentions, it's a crude regular expression for an email address, though is an encompassing expression to find any valid email.
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