What is wrong with my script?

I've made this small script that is supposed to check if two strings are identical or not.

It works as expected, however if the strings contain a parenthesis "(" it stops working.

Why is that?

Working example:

jsfiddle.net/kjmwvhgL/1/

Attached: MyScript.png (524x402, 13K)

Other urls found in this thread:

jsfiddle.net/kjmwvhgL/13/
twitter.com/SFWRedditImages

> What is wrong with my script?
it's javascript

if ur using javascript dont reinvent the wheel just go get lodash or some bullshit

You don’t need to use a regex to check equality of primatives.

Just check if str === str2

In this case I need to use regex to capture the strings I want to compare.

I've tried to store the regex matches as two variables and compare them with str === str2 but that doesn't work?

>if (findtostr = find2)
That's an assignment, not a comparison.

I cant be assed to interpret that code but

#!/bin/sh
[ "$1" == "$2" ] && echo "It's a match!" || echo "It's NOT a match!"

Aside from the assignment/comparison thing, you're passing the first string to the match method, which is expecting a regular expression. If your first string contains a single parenthesis, it becomes an invalid regular expression and .match throws.

>That's an assignment, not a comparison.

But it works though? Except when the string contains a ( of course.

I mean, it is an assigment, It will evaluate as true no matter what, you're not evaluating anything at all, to compare you either use == loose or === strict, user.

I'm not a JS wizard but I guess it is the equivalent to SQL injection.

The string is not being treated strictly as text. Instead it tries to execute whatever you put inside the parenthesis

kek

>It will evaluate as true no matter what
No it won't. It will return the right hand side. If the strings don't match, find2 will be null, so the assignment will actually be falsey.
So it'll kind of work, for really stupid reasons

>So it'll kind of work, for really stupid reasons
This is like the motto of JavaScript.

I figured something like that was the culprit. What I'm trying to do is capture the first line in the first textarea, then use that result in a .match on the whole text in the second textare. So if there is an exact match anywhere in the second textarea, it should match it.

I figured if I used .toStrong on the result of the .match on the first textarea, it would convert the matched text to a string and use it to perform the .match..

.toString

Sorry for the typo

That's a misuse of match. Use .includes instead

jsfiddle.net/kjmwvhgL/13/

The first match should also probably be str.split('\n')[0] instead of str.match(/.+/), but whatever

Just because it works for your case doesn't mean it's not wrong. The correct way to do comparison is always with ===.

As for the parenthesis thing, the console seems to complain about unterminated parentheticals. I'm not sure why it's performing a syntax check on arbitrary input strings.

It's also the motto of my adventures in JavaScript so far. As you can probably see, I conjured this straight out of my own imagination with my limited self-taught JavaScript skills.

Attached: 1542813740544.png (960x960, 617K)

see .match expects a regular expression, and will try to compile its input into a regular expression if it's a string. An invalid regular expression will cause it to throw.

JavaScript "devs", everybody.

>then use that result in a .match
Bad line of thinking. What if I enter in textbox1 a valid regex, and textbox2 a string that matches that regex? It's going to return true, even though the strings are clearly different.

What you want is to create two new strings based on your regex .match, then compare those strings for equality.

>to compare you either use == loose or === strict, user.
Alright, I'm taking notes. I really appreciate the feedback.

Makes sense, I suppose what threw me off was that is appeared to kinda work, but now that i realize why it behaves that way it makes sense why it doesn't work.

function hent() {
var str = document.getElementById("input").value;
var str2 = document.getElementById("input2").value;

if (str === str2) {
alert("Match");
}
else {
alert("No match");
}
}

This is what you want, OP.

No, it's not what he wants. Read the thread.

Use indexOf.

Stop using assignment to check equality REEEEEEEEEEEEEEEEEEEEEEEEE

I'm sorry. I'll re-read the thread now.

Wtf is going on here, is this bait?

Remember that .match returns an array of matches (or null if there are none), not a string.

I've deleted this. I'll try again, hold on.

Why is your else so far away from your if statement

Attached: standby.jpg (267x200, 17K)

I don't get it

Ask stack overflow not Jow Forums

x-D

if (str2.indexOf(str1.split('\n')[0]) !== -1)

Why use regex for something so simple?

Beautiful!

Thank you, I learned something today.

Attached: 1442187528822.jpg (918x1224, 164K)

alert(`It's ${str2.includes(str.split('\n')[0])?'':'NOT '}a match!`);