mrEmpty
mrEmpty

Reputation: 841

Select first complete string matching partial string

I have a page which contains many tags with a string in them, for example 'Am I a String?  Yes'.

Using JQuery, I want to get the first instance of 'Am I a String?  ' and then take the Yes or No after that and store it so I can use it in a conditional.

Any ideas?

Thanks.

The code I'm going with:

function experimentThree() {
            var stringBool = $('*:contains("Was it enough?  ")');
            console.log('stringBool = : ' + stringBool);
            console.log('stringBool Object 1 = : ' + stringBool[0]);
        }

Thinking if I can get the complete string in the first object I can compare that to what I expect to see.

Upvotes: 0

Views: 83

Answers (2)

Ihor Shubin
Ihor Shubin

Reputation: 10726

for example:

var a = $('div').text().match(/Am I a String\? (Yes|No)/g)[0];
var b = a.match(/(Yes|No)/g)[0];

Upvotes: 0

Bilal Murtaza
Bilal Murtaza

Reputation: 795

check this out

$('body').find('*:contains("Am I a String")').each(function(index, crntNode){
    var parts = $(crntNode).text().split('?');
    var flag = parts[1].trim();
    alert(flag);
});

Upvotes: 2

Related Questions