views:

41

answers:

3

Given:

var value = $this.text();

Where value equals: Phasellus pellentesque metus in nulla. Praesent euismod scelerisque diam. Morbi erat turpis, lobortis in, consequat nec, lacinia sed, enim. Curabitur nisl nisl, consectetuer ac, eleifend a, condimentum vel, sem.

When the user types in: Where value equals: Phasellus pellentesque metus in nulla. Praesent euismod scelerisque diam. Morbi erat turpis, lobortis in, consequat nec, lacinia sed, enim. Curabitur nisl nisl, consectetuer ac, eleifend a, condimentum vel, sem.!!!

The 3 !!!

I want JavaScript to Alert, allowing me to call a different function.

I'm trying to use:

if (/!!!$/.test(value)) {} but that doesn't seem to be working.

Ideas?

+1  A: 

It looks like there is a \n after the !!!. Use /m flag for making the regex multiline.

if (/!!!$/m.test(value)) {
    console.log("it works");
} 

Check this:

var s = "When the user tl, sem.The 3 !!!";

if (/!!!$/m.test(s)) 
    console.log("multiline matches");   //prints

if (/!!!$/.test(s)) 
    console.log("single line matches"); //prints

s += "\n";

if (/!!!$/m.test(s)) 
    console.log("multiline matches");   //prints

if (/!!!$/.test(s)) 
    console.log("single line matches"); //doesn't print
Amarghosh
A: 

this code will note work your conversion to jquery object is not right, use the following

var value = $(this).text();
Kronass
A: 

$this doesnt refer to self. $(this) refers to self. so it should be like:

var value = $(this).text();

full eg.:

<body>
    <p>hello!!!</p>
    <p>done</p>
</body>
<script>
    $(document).ready( function()
    {
        $("p").click( function()
        {
            var value = $(this).text();
            if (/!!!$/.test(value)) { msg = 'done';}else{ msg = 'not done';}
            alert(msg);
        });
    });
</script>
KoolKabin