Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I need to create a regex pattern which starts with an i and followed with 6 numbers. for example: i123456, i098765.
I have followed this SO, and created this regex: ^\[i]{1}[0-9]{6}$ but in this validation web site I got an false for the examples above.
How do I achieve this?
Things to note:
The caret (^) and dollar sign ($) characters at the beginning and end denote the start and end of the line or string respectively, depending on the multiline (m) flag. If you are looking for matches within a string, this regex will fail because it requires the i123456 pattern to be the whole string (or line).
You do not need to make a character set, as denoted by including characters in square brackets, for matching a single character. Just write it plainliy.
A literal RegExp begins and ends with forward slashes. Some online tools and validators might not recognize your expression as valid unless you include them.
As #kamilkp responded, /^i\d{6}$/ would be the shortest expression possible if you are attempting to validate the whole input string against the pattern. If you are trying to pluck substrings that match the pattern from a larger string, the simplest pattern would be /i\d{6}/ or /i\d{6}/i if the case of the leading letter 'i' is irrelevant.
The shortest regex that would do it is: /^i\d{6}$/
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I am trying to calculate the hash of a string. And I found that the result changes depending on the method I use.
In this webpage https://codebeautify.org/md5-hash-generator.
If I use the form writing the string in the text field:
16120&{"number":"4545","params":"{\"locale\":\"en_EN\"}"}
I get 26528d6e0e802d5569e2e03fde0a825c. However if I do
CryptoJS.MD5('16120&{"number":"4545","params":"{\"locale\":\"en_EN\"}"}').toString();
the result is fe31f378efcc4ae4de71e70278991741.
If I use a simple string like 1234 I get the same result but using the one above I don't so I guess the problem is escaped bars or something but I can't find a solution.
In your JS code, JS is escaping the quotes. The website is not escaping. So you are hashing different things, hence different hash results.
The Backslashes for escaping the string are only ok in-code because they will not be part of the resulting string. See w3schools -> chapter "Escape Character".
Try executing console.log('\"') to understand this, it results in logging only a single doublequote (").
So if you remove these escape characters and insert the string in the input (where escaping is neither needed nor supported), the hash will be equal.
The backslashes in the second case need to be doubled up. Once for JavaScript and once for JSON.
CryptoJS.MD5('16120&{"number":"4545","params":"{\\"locale\\":\\"en_EN\\"}"}').toString();
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 5 years ago.
Improve this question
How could I replace or remove all the image tag in my string using javascript?
Coffee Bean<div><br /></div><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX.." />
I want to remove the image and the successive image tag in a string. How to do it?
Using Regex:
myString.replace(/<img[^>]*>/g,"");
[^>]* means any number of characters other than >. If you use .+ instead, if there are multiple tags the replace operation removes them all at once, including any content between them. Operations are greedy by default, meaning they use the largest possible valid match.
/g at the end means replace all occurrences (by default, it only removes the first occurrence).
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, '');
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 :
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.