Same function different MD5 hash results [closed] - javascript

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

Related

Expanding ${var} references in a JavaScript string [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 4 years ago.
Improve this question
Learning something beyond vanilla JavaScript and the book I'm reading is telling me a statement like:
let someVar = 'Happy';
console.log('I hope you have a ${someVar} day.');
Should display 'Happy'? in the log or an alert or possibly anywhere.
It doesn't work. I'm using FireFox Dev Ed and I just get a line with the entire:
${someVar}
in it. Any guidance... Is this a weird transpiler issue or ES6+ issue?
You need backticks surrounding the string there in order for the interpreter to properly interpret it as a template literal.
let someVar = 'Happy';
console.log(`I hope you have a ${someVar} day.`);
When you have a normal string, you can use single quotes ' or double quotes ", but when you're using a template literal, you must always use backticks `.
You can also use backticks anyway even if you aren't interpolating any variables inside, just so you don't have to escape quote characters, for example.

Removing all characters after specific character [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 6 years ago.
Improve this question
I know this is a simple task, but my Javascript knowledge is (very) limited and I can't seem to get this to work. I've researched it thoroughly and have read through several similar questions here before posting, but still no luck. I'm trying to remove all characters after the comma within a particular div. My poor understanding of Javascript (and programming in general) is certainly limiting me here.
var s = document.getelementsbyclassname('menu-staff');
s = s.substring(0, s.indexOf(','));
document.write(s);
<div class="menu-staff">Value One, Value Two, Value Three, Value Four</div>
I can get this to work for a static string stored in the variable, so I know the Javascript function works, it must simply be a problem with targeting the div by class, right? Thanks for bearing with a newbie. I fully expect this to be flagged as a duplicate and removed in a matter of hours, but hopefully someone will be kind enough to guide an ignorant designer towards the light. :)
Split your string into parts and then just output the first one.
var str = document.querySelector('.menu-staff').innerHTML;
console.log(str);
var parts = str.split(',');
document.write(parts[0]); //Pssst... You shouldn't use document.write. Manipulate the DOm instead.
<div class="menu-staff">Value One, Value Two, Value Three, Value Four</div>
You could split pieces of your string separated by a comma into an array and just keep the first element :
var s = "bonjour, et bienvenu";
console.log(s.split(",")[0]); // bonjour

Javascript replace() to remove single quote produces weird result [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 7 years ago.
Improve this question
With this code
"test\536".replace(/'/g, "")
I would expect there is no different to the original string, because there is no single quote. But I get this instead
"test+6"
When I run this on a string with single quote, it works as expected
"test'536".replace(/'/g, "")
"test536"
The problem is in your string, in JavaScript strings \ is used to escape the following character. so if you want to prevent this behavior you should escape it using another slash, it will be \\:
"test\\536".replace(/'/g, "")
console.log("test\\536".replace(/'/g, ""));
console.log("test'536".replace(/'/g, ""));
Hope this helps.

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 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