Regular Expression Chart Generator
I'm trying to make a regex pattern that will match this:
Name[0]/Something
or
Name/Something
The name of the verbs and something will always be known. I did for Name[0]/Something
but I want to create a pattern for this verb in one regex I tried sign [0]
as optional but it didn't work:
var regexPattern = "Name" + @"\([\d*\]?)/" + "Something"
Do you know a generator where I will enter some verbs and it will make a template for me?
source to share
Use this:
Name(\[\d+\])?\/Something
-
\d+
allows one or more digits -
\[\d+\]
allows you to enter one or more digits within[
and]
. So he will allow[0]
,[12]
etc., but will reject[]
-
(\[\d+\])?
allows the digit with brackets to be present either at zero or at one time -
\/
indicates a forward slash (only one) -
Name
andSomething
are string literals
source to share
var string = 'Name[0]/Something';
var regex = /^(Name)(\[\d*\])?\/Something$/;
console.log(regex.test(string));
string = 'Name/Something';
console.log(regex.test(string));
You are wrong with this pattern: \([\d*\]?)/
-
Don't need to use
\
before(
(in this case) -
?
after]
means: character]
zero or one time
So, if you want the template to [...]
render zero or one time, you can try:(\[\d*\])?
Hope this helps!
source to share