user3518221
user3518221

Reputation: 97

match part of a string

I need to identify list (li) items that include a full stop at the very end.

I can work out how to match a full string with

if ($(obj).elements[0].innerHTML === "This list has full stops."

but don't know how to match just a full stop at the end. I tried

if ($(obj).elements[0].innerHTML === "*."

but no good.

Please help

Upvotes: 0

Views: 56

Answers (2)

Nabeel Bape
Nabeel Bape

Reputation: 60

Not a big fan of regex, however if you want to do the above thing multiple times and make code easily readable you can create a function for it.

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

And use it like this:

var str = $(obj).elements[0].innerHTML;
if(endsWith(str,'.')){
  // Do watever.
}

Upvotes: 0

OneOfOne
OneOfOne

Reputation: 99225

if (/.*?\.$/.test($(obj).elements[0].innerHTML) {}

Upvotes: 3

Related Questions