Regex Clarification [closed] - javascript

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 :

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 pattern with special characters, digits and letters all combined [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 4 years ago.
Improve this question
Say I have a regex pattern like this:
/^\*HELLO\*/
Just looking for the string "*HELLO*". But then I completely want to change it up so I do this:
/^\*&$&^*2#H\*/
Now I'm looking for the string "*&$&^*2#H*".
How should I change my regex pattern to check for such a complex string with all those different characters?
You should escape the special characters in your pattern, wich are used as tokens by Regex, such as *,^ and $. Or you will end up with an error claiming about a wrong pattern in your regex.
This is how should be your regex: /\*&\$&\^\*2#H\*/.
Furthermore if you are searching for the string with .indexOf() or .includes() methods, you can just pass the string as it is.
str.indexOf("^\*&$&^*2#H\*");

Regex describing a regex pattern? [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 6 years ago.
Improve this question
I am surprised that an answer to this is not easily found.
I am in the process of making a JSON schema. I have an object and one of it's properties is a string containing a regex pattern. This property must contain only regex.
So, this question is realistically two questions in one:
What is the regex pattern that describes regex patterns (javascript-compatible please)?
Secondly, how do I apply this to JSON schema (in the "pattern" property or even in the "patternProperty"1 property)?
1: I have no idea what purpose would require you to apply this in "patternProperty", but someone out there could find it useful
NOTE: Since, JSONschema is JSON and JSON is JavaScript-based, JavaScript scripters may find the solution (to the JSONschema-side of the question), as well as the problem, useful in their scripts.
Regexes can have nested parentheses.
Therefore, they are not describable by a regular expression.
Sorry.

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 expression for Apostrophe [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anybody suggest what would be regexp for String, which validates following conditions:
Can contain characters a-z or A-Z. At least one should be there.
Can contain (space), '(apostrophe), -(hyphen), .(dot)
Anything other than this set, such as special character or number or anything else, would be an invalid character.
Something like this should work
/^[a-zA-Z' \.\-]*[a-zA-Z]+[a-zA-Z' \.\-]*$/
Which translates to 'at least one letter surrounded by zero or more of any valid character'.
I usually don't answer gimme codez questions but hey it's Friday!
There are perhaps thousands of questions here on SO following the same pattern:
Plz help me with a regular expression for a string that
- _must_ contain at least one X
- _can_ contain Y
and the answer is usually something like
/^ Y* X [XY]* $/
or if you're fancy
/^ (?=.*X) [XY]+ $
Unfortunately, all these answers (or, rather, these questions) are wrong. The problem, as usual, is that the specs are incorrect - the asker takes some "good" examples and describes them in the question, but doesn't realize that this description also matches many "bad" cases. When taken literally, this question will be answered with an expression that only does a half of its work - yes, it does validate good cases, but it does not reject bad ones. A good expression must do both!
Example: I want to validate a telefone number, which is something like 123 or 123-456-789. So I post a question on SO:
Plz help me with a regular expression:
- must contain at least one digit
- can contain a dash
and in a few seconds I get
/^-*\d[\d-]*$/
which I test with my examples (works!) and insert in my code. On the next morning, to my deepest embarassment, someone registers on my site providing this "telefone number":
----------3-----------
The moral of the story: never validate "strings". Validate domain objects!
To answer this specific question: I can't provide you with a good regular expression until you tell me what it is for.

Categories