0

I'm trying to compare value of text inside div (This is a sentence.) and text defined in js variable:

function isSame(){
s="This is a sentence."
    var text1 = $('#right').text();
    var t1 =  text1.replace(/ /g,'').replace(/&nbsp;/g, '').replace(/\<br\s*[\/]?>/gi, '').replace('\t','');
    var s1 = s.replace(/ /g,'').replace(/&nbsp;/g, '').replace(/\<br\s*[\/]?>/gi, '').replace('\t','');
    console.log(s1+" VS "+ t1);
    if (t1 == s1){
        console.log("Same");
    } else {
        console.log("Not same...");
    }
}

All the .replace are because on console I had extra tabs in div (which has style in it) I had extra spaces. Console log shows:

Thisisasentence. VS 

Thisisasentence.

Not same... 

What is it I'm missing?

4
  • It would be worth posting the HTML as well. Commented Apr 5, 2016 at 16:28
  • looks like there are two line breaks at the start of t1 Commented Apr 5, 2016 at 16:29
  • @DBS html is: <div id="right">This is a sentence. </div> Commented Apr 5, 2016 at 16:39
  • @Mark, I think I removed them after Rob's suggestion Commented Apr 5, 2016 at 16:39

3 Answers 3

2

Instead of this entire regular expression, have you tried using the trim() method?

As stated in the documentation for String.prototype.trim(), in MDN:

The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

I believe your code should then be reduced to:

function isSame() {
    var s = "This is a sentence.";
    var text1 = $('#right').text();

    console.log(s1 + " VS " + t1);
    if (text1.trim() === s1) {
        console.log("Same");
    } else {
        console.log("Not the same...");
    }
}

And the comparison would work as expected.


Update:

As already mentioned in further answers by Ysharp and Rob Brander, you could increment your regular expression by expanding it to other match new lines and carriage return elements. That would change your current regex by adding a \s+ matcher to it, resulting in:

replace(/\s+/g, '')
Sign up to request clarification or add additional context in comments.

Comments

0

Your regular expressions look like they're trying to replace any whitespace characters. I would suggest using \s as part of your regular expression, because that looks for all permutations of whitespace.

Your two strings are not equal because there is a newline before and after the phrase. You could try replacing just the new lines with .replace('\n', '')

1 Comment

Hi Rob, thanks for the \s and \n suggestion. Console now shows both of them in same line, however it's still showing Not same.
0

You tried to get rid of whitespace using

replace(/ /g, '')

but as others pointed out, this is not sufficient to get rid of carriage returns and/or newlines.

Try this instead:

replace(/\s+/g, '')

which will take care of stripping out all of the '\t', '\n', etc, everywhere in the strings it is applied to.

'HTH,

1 Comment

Thank you, does work but ended up using trim, wish I could vote 2 correct answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.