My attempt was :
var re = new RegExp("\w{" + n + "}", "g");
But it didn't seems to work.
P.S. - I have searched several questions of Stackoverflow thinking it must have been asked before But I didn't find one, so I asked my question.
The problem is that \ is not only the escape character in regex but also in JS strings. So when you create a regular expression from a string you need to escape it. This means that \w becomes "\\w" in a string and if you want to match a single \ it would even become "\\\\".
Instead of changing it to \\w you can also use . if you don't care about the characters or if the string was validated before.
Related
I tried to search this question first but didn't found what I need, here is it:
I have a string like: "substring1/substring2.substring3" (e.g. "library/History.read")
I need a regular expression to check:
the substring1 must be "library"
the substring2 must have a captial letter at the beginning
the substring3 must be "read"
I'm not familiar with regular expression, the current I wrote is:
'library/([a-zA-Z])\\.(read)' but it is not correct. Please help me, thanks!
There are many ways to solve regex problems, starting from your own attempt here's something that works! :-)
library/([A-Z][a-zA-Z]+)\.(read)
You had three small mistakes:
. is a character class matching any character except newline. Escape your . like this \. not like this \\.; and
Your first capture group was matching exactly one letter. You missed the quantifier. Use the + qunatifier to match one or more.
As pointed out by Peter, you were missing a character class for your first capital letter
Try this regex:
testStrings = ["library/History.read", "library/other.read", "library/Geography.read", "library/History.unread", "ebook/hstory.read", "library/StackOverflow.read"]
regex = /library\/[A-Z][^\.]*\.read/
testStrings.forEach(testString => {
console.log(regex.test(testString) + " - " + testString)
})
I'm trying to do a search for a character in a string NOT matching the regex :
password.search(/[`!###$%^&*A-Za-z0-9]/i));.
Basically, all characters that aren't this regex isn't allowed and I want to know if the user has input any characters that isn't allowed. For example, '\', or any other characters that I might not think of.
I'm pretty sure there's a question similar to this out somewhere, but despite trying to look for it I surprisingly couldn't find it. If this is a duplicate question please link me.
According to this answer, you could use ?!:
console.log("valid$\\".search(/(?![`!###$%^&*A-Za-z0-9])/i));
console.log("256)128".search(/(?![`!###$%^&*A-Za-z0-9])/i));
f you want to exclude a set of characters (some punctuation characters, for example) you would use the ^ operator at the beginning of a character set, in a regex .
I have a pattern to search around web but im new to it and unable to verify it.
Im looking for example
[verify if any whitespace] [any of this char ':' '|' ';'] [verify if any whitespace] [[String a-zA-Z0-9-]+]
Suppose Test String -
" : hello129 " or ":hello129" or ";hello129" or "|hello129" or " | hello129"
My attemps
\s[:;|]\s[a-zA-Z0-9_.+-]+
(\w+\s\w+):(\w+\s\w+)[a-zA-Z0-9_.+-]+
Please suggest me possible solutions for this pattern in regex/regular expressions
Thank you in advance :)
Whitespace is represented with \s. The other groups are easily written in brackets.
Whitespace could by one or more characters, so the + modifier will be necessary. If Whitespace was optional, the * would have been okay as well. If only one character of whitespace would be allowed, we would leave the modifier out.
The string in the end is one or more characters long and needs the + as well.
The result is a regular expression like this:
\s+[:;|]\s+[a-zA-Z0-9-]+
Here is an example including tests on the great RegEx testing site regex101.com.
I don't have much experience with JavaScript but i'm trying to create a tag system which, instead of using # or #, would use /.
var start = /#/ig; // # Match
var word = /#(\w+)/ig; //#abc Match
How could I use a / instead of the #. I've tried doing var slash = '/' and adding + slash +, but that failed.
You can escape it like this.
/\//ig; // Matches /
or just use indexOf
if(str.indexOf("/") > -1)
You need to escape the / with a \.
/\//ig // matches /
You can escape it by preceding it with a \ (making it \/), or you could use new RegExp('/') to avoid escaping the regex.
See example in JSFiddle.
'/'.match(/\//) // matches /
'/'.match(new RegExp('/') // matches /
If you want to use / you need to escape it with a \
var word = /\/(\w+)/ig;
In regular expressions, "/" is a special character which needs to be escaped (AKA flagged by placing a \ before it thus negating any specialized function it might serve).
Here's what you need:
var word = /\/(\w+)/ig; // /abc Match
Read up on RegEx special characters here: http://www.regular-expressions.info/characters.html
You can also work around special JS handling of the forward slash by enclosing it in a character group, like so:
const start = /[/]/g;
"/dev/null".match(start) // => ["/", "/"]
const word = /[/](\w+)/ig;
"/dev/null".match(word) // => ["/dev", "/null"]
I encountered two issues related to the foregoing, when extracting text delimited by \ and /, and found a solution that fits both, other than using new RegExp, which requires \\\\ at the start. These findings are in Chrome and IE11.
The regular expression
/\\(.*)\//g
does not work. I think the // is interpreted as the start of a comment in spite of the escape character. The regular expression (equally valid in my case though not in general)
/\b/\\(.*)\/\b/g
does not work either. I think the second / terminates the regular expression in spite of the escape character.
What does work for me is to represent / as \x2F, which is the hexadecimal representation of /. I think that's more efficient and understandable than using new RegExp, but of course it needs a comment to identify the hex code.
Forward Slash is special character so,you have to add a backslash before forward slash to make it work
$patterm = "/[0-9]{2}+(?:-|.|\/)+[a-zA-Z]{3}+(?:-|.|\/)+[0-9]{4}/";
where / represents search for /
In this way you
For me, I was trying to match on the / in a date in C#. I did it just by using (\/):
string pattern = "([0-9])([0-9])?(\/)([0-9])([0-9])?(\/)(\d{4})";
string text = "Start Date: 4/1/2018";
Match m = Regex.Match(text, pattern);
if (m.Success)
{
Console.WriteLine(match.Groups[0].Value); // 4/1/2018
}
else
{
Console.WriteLine("Not Found!");
}
JavaScript should also be able to similarly use (\/).
I don't have much experience with JavaScript but i'm trying to create a tag system which, instead of using # or #, would use /.
var start = /#/ig; // # Match
var word = /#(\w+)/ig; //#abc Match
How could I use a / instead of the #. I've tried doing var slash = '/' and adding + slash +, but that failed.
You can escape it like this.
/\//ig; // Matches /
or just use indexOf
if(str.indexOf("/") > -1)
You need to escape the / with a \.
/\//ig // matches /
You can escape it by preceding it with a \ (making it \/), or you could use new RegExp('/') to avoid escaping the regex.
See example in JSFiddle.
'/'.match(/\//) // matches /
'/'.match(new RegExp('/') // matches /
If you want to use / you need to escape it with a \
var word = /\/(\w+)/ig;
In regular expressions, "/" is a special character which needs to be escaped (AKA flagged by placing a \ before it thus negating any specialized function it might serve).
Here's what you need:
var word = /\/(\w+)/ig; // /abc Match
Read up on RegEx special characters here: http://www.regular-expressions.info/characters.html
You can also work around special JS handling of the forward slash by enclosing it in a character group, like so:
const start = /[/]/g;
"/dev/null".match(start) // => ["/", "/"]
const word = /[/](\w+)/ig;
"/dev/null".match(word) // => ["/dev", "/null"]
I encountered two issues related to the foregoing, when extracting text delimited by \ and /, and found a solution that fits both, other than using new RegExp, which requires \\\\ at the start. These findings are in Chrome and IE11.
The regular expression
/\\(.*)\//g
does not work. I think the // is interpreted as the start of a comment in spite of the escape character. The regular expression (equally valid in my case though not in general)
/\b/\\(.*)\/\b/g
does not work either. I think the second / terminates the regular expression in spite of the escape character.
What does work for me is to represent / as \x2F, which is the hexadecimal representation of /. I think that's more efficient and understandable than using new RegExp, but of course it needs a comment to identify the hex code.
Forward Slash is special character so,you have to add a backslash before forward slash to make it work
$patterm = "/[0-9]{2}+(?:-|.|\/)+[a-zA-Z]{3}+(?:-|.|\/)+[0-9]{4}/";
where / represents search for /
In this way you
For me, I was trying to match on the / in a date in C#. I did it just by using (\/):
string pattern = "([0-9])([0-9])?(\/)([0-9])([0-9])?(\/)(\d{4})";
string text = "Start Date: 4/1/2018";
Match m = Regex.Match(text, pattern);
if (m.Success)
{
Console.WriteLine(match.Groups[0].Value); // 4/1/2018
}
else
{
Console.WriteLine("Not Found!");
}
JavaScript should also be able to similarly use (\/).