This question already has answers here:
Matching a Forward Slash with a regex
(9 answers)
Closed 6 years ago.
How do I make a Slash able to be used in this Metachar String:
/#(\w+)\b/gi
That is supposed to find the "Text"(#text) This is a test #Text I agree
And it does. But now I wan't the same thing for somthing that uses a
/
You need to escape the slash so it is not interpreted as denoting special meaning. Escaping means prefixing with a backslash, so you just need two together. Adapting your existing example:
/#([\w\/]+)\b/gi
You're now allowing alphanumeric and slash characters (hence the need for a "range" of characters, denoted by square brackets.)
This one will do it: Try:(will match /Text)
/\/(\w+)\b/gi
Related
This question already has answers here:
Regular expression to match a dot
(7 answers)
Closed 2 years ago.
Using regex, I'm trying to check whether there's only 2-4 characters used after a .. At the moment, i got it working to detect when its less than 2 characters but after 4 characters, it still deems it as successful. How can I fix this? This is what I have written down:
/.[a-zA-Z]{2,4}$/
You need to escape the dot (.).
/\.[a-zA-Z]{2,4}$/
. is the control character in RegExp, you should to escape it:
/\.[a-zA-Z]{2,4}$/
And, to ignore case, add the i flag:
/\.[a-z]{2,4}$/i
This question already has answers here:
regex to match a single character that is anything but a space
(3 answers)
Closed 4 years ago.
How to check in JavaScript if the specified character is not a whitespace using regex only? Right now I am doing something like the code below with negation ! but I would like to avoid mixing of two things to avoid confusions.
if (!/\s/.test(character))
console.log('this is not a whitespace');
if (/\S/.test(character))
console.log('this is not a whitespace');
Use the negated set notation in the regex
/[^\s]/
That will match everything that isnt a whitespace.
This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Match empty string, comma, hyphen or underscore once using regex
(2 answers)
Closed 4 years ago.
I have a requirement to do a field validation using regEx. The requirement is not to have any special characters. It is working for me for a single white space, but not for multiple white spaces. It does not work if there is a white space at the beginning. Can you please help me with this?
This is what I used:
^(\w+ ?)*$
You might be looking for
^[\w ]*$
The ^ and $ are anchors for the start/end of the string, the [...] is called a character class and would allow only [A-Za-z0-9_ ]. The * is a quantifier and means zero or more times, thus the expression would also allow an empty string. If this is not what you want, change it to + instead of the *. Please note that this would also allow a string with only spaces (it really depends on what you want).
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:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I have a regular expression that matches strings that start with either the word "db" or the word "admin":
/^\/(db|admin)\//
I'm refactoring my code and my requirements have changed: Now I need the opposite, i.e. a regular expression that matches strings that don't start with one of those two words. Is this possible with regular expressions?
Note: I cannot use JS API - the regular expression is inserted in Express.js's app.all(path, callback) method directly (as the path).
Thanks to Nico for pointing out that JavaScript RegExp has (?!) functionality. The solution seems to be:
/^\/(?!admin|db)/