javascript regexp pattern definition [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need help
I have a form feild I need to add validation to. The format of the value has to be specific. I want to define a regex pattern to handle it.
The format needs to allow L123456
L = it has to be L which is the start of out skus
123456 = I need to confirm they are entering a 6 digit number.
thanks
Jeff

var str = "L123456";
/^L\d{6}$/.test(str)
The pattern uses the ^ to determine the strings starts with. \d{6} states 6 digits, and $ means end of string. See: http://www.regular-expressions.info/ for more info.

Related

Regex time validation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So i want to make a regular expression to validate times between 12:00 and 22:00 but i cant get my head around making expressions and cant find any examples online that i can just swap out examples of. Can anybody help me?
You can use the following regular expression:
^((1[2-9]|2[0-1]):[0-5][0-9]|22:00)$
This is how it works:
^ and $ match start and end of string, and they are there to prevent matching also 112:009 for example (which contains 12:00).
[2-9] matches numbers between 2 and 9, and (a|b) would match either a or b.
Debuggex Demo

Regex to avoid spaces AND a dot followed by hyphen or hyphen followed by a dot [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need a regex for an input pattern that doesn't allow spaces and a "." followed by a "-"
i.e.:
this-is-valid.com
this is not valid
this.-is-also-not-valid
this-.is-also-not-valid
The key will be defining the group of characters that are allowed on the left and right sides and just adding "-" to the right-hand-side. I've just used \w here:
\b((?:\w|-)+\.\w+)(?:[^-]|$)
[Live Example]

Escape "+" in JavaScript Regular Expression [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is the code I am trying to execute
input.replace(/^.+?(?=\+)/, ''), "i")
I have escape plus with \+ but I get error
Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat
Kindly let me know how to escape + in the above regexp.
you need to put the i modifier at the end of the regex - not as a separate parameter. For example:
input.replace(/^.+?(?=\+)/i, '');
As #LorenzMeyer has pointed out, you don't actually need the i modifier, because the case is irrelevant based on your regex. Perhaps you need a global replace? In which case your replace would look like this:
input.replace(/^.+?(?=\+)/g, '');

Regex pattern to validate phone not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Just trying to create a simple Regex validation with JavaScript. I want to validate the phone number in the following format "+91-xxx-xxx-xxx", where the country code is fixed.
I have used the following regex pattern
/^[91]-\d{3}-\d{3}-\d{3}$/
But it isn't working. Here is my jsfiddle
Can you help?
You need to add + at the start and also you have to remove the character class which has 91
^\+91-\d{3}-\d{3}-\d{3}$
DEMO
This [91] would match a single character either, 9 or 1 but not 91. To match 91, you need to getout the number 91 from the character class. + is a meta character in regex, you need to escape it to match a literal + symbol.

Regex Clarification [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am relatively new to regex. I found this regex being used and was not able to understand how exactly it is working.
([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$/
I understand most of the basic syntax but not able to understand what this means.
Any clarity on this will be appreciated! This is used in javascript so the tag.
Thanks!
The same without uneeded characters:
/(\(*(\w\/?\w?(%)?(,)?)*|[\/.]|\w([-*+\\<>=](\(*\w\/?(%)?\)?\)?(,)?)+)*\)?)*$/
This pattern can match this kind of string (or nothing):
(((a/b%,(((a/b%,///./././(((a/b%,k*((((((((P/%)),)
your regex:
([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$/
can be visualized as:
Debuggex Demo
unless there is a typo in your regex, it will never match anything; the regex ends with $/ which means end of the string followed by /, unless you are matching over multiple lines. If this was homework, I would say the teacher is making a bad joke because of the $/ that doesn't usually match anything.
After some simplification you get:
(\(*(\w\/?\w?(%)?(,)?)*|[\/.]|\w([-*|+\\<>=](\(*\w\/?(%)?\)?\)?(,)?)+)*\)?)*$/
If you don't care about grouping, then this is similar:
(\(|[\/.]|(\w\/?\w?%?,?)|\w([-*|+\\<>=](\(*\w\/?%?\)?\)?,?)+)*\)?)*$/
Debuggex Demo
this takes advantage that (a*|b*)* can be simplified into (a|b)*
To learn more about regex REFER : regex101 give your code there and read explanation .
Your regex: ([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$
Will match anything like
(d/f%,
something
122
But will fail to match something like
--{}
Explanation :

Categories