Jonathan Wood
Jonathan Wood

Reputation: 67283

How to Find HTML-Encoded Character in JavaScript String?

How would you go about finding a string constant that is not HTML-safe?

It appears the following searches for the individual characters.

var i = text.indexOf('»')

Upvotes: 2

Views: 679

Answers (3)

ngn
ngn

Reputation: 7902

You'll first need to unescape the HTML in the pattern:

var i = text.indexOf(decodeHTML('»'));

function decodeHTML(s) { // e.g. using jQuery
    return $('<div>' + s + '</div>').text();
}

Upvotes: 1

John Stimac
John Stimac

Reputation: 5491

var i = text.indexOf('\273')

would search for the actual character.

http://www.c-point.com/javascript_tutorial/special_characters.htm explains how to escape special characters in javascript.

Upvotes: 0

Byron Whitlock
Byron Whitlock

Reputation: 53921

Try looking at the html directly.

document.getElementById('search').innerHTML.indexOf('&raquo;')

Upvotes: 0

Related Questions