Regex expression for Apostrophe [closed] - javascript

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.

Related

Same function different MD5 hash results [closed]

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();

javascript Regex pattern validation [closed]

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}$/

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 :

Need help to create one regex for password validation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need one regex which validates below condition for password entered by user:
it must have at least one capital letter
it must have at least one number
It must have at least one special character
I need regex to implement it in javascript.
I have tried this one
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*()_+|~-=\`{}[]:";'<>?,])/i;
Thanks in Advance
For the rules you mentioned:
/^(?=.*[0-9])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])/;
This should suffice.
[^a-zA-Z0-9] matches anything that is not an alphabet and not a number, i.e. a special character.
I removed the case insensitive flag because you specifically mentioned there should be 1 uppercase letter.

Is there a good way to identify a word, even if it's misspelled in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a bunch of words (mostly locations, like Stockholm and London) and a bunch of strings. like "I want to go from stockholm to london". I want to find out what words are in what strings.
I currently use .indexOf to perform this task.
Is there a library, method, function etc, in javascript that identifies misspelled versions of words?
https://github.com/epeli/underscore.string#readme
Check out the levenshtein _.levenshtein(string1, string2) distance function. It can be used to calculate the distance between too strings.
I found this JS library http://www.javascriptspellcheck.com/ that is supposed to check spelling in several languages
After reading the above I'm not sure if I'm exactly clear on what you are trying to do... but regarding your final question about identifying words which are incorrectly spelled -- I'd take a look at java script spell check. And as a side note.. Often people try and use soundex when they want to count words which are spelled both correctly and incorrectly.
php by default has levenshtein function. you can use this method by using PHPJS library.
by finding the levenshtein distance between the word and a dictionary of correct words you can obtain a word with least levenshtein distance from the miss-spelled word. This would most probably be the correct spelling for a particular word.
In the past, I have used MissPlete library, which uses Jaro–Winkler distance algoritm by default.
It has no dependencies (not even jQuery), which I highly value.

Categories