kambamsu
kambamsu

Reputation: 21

Search document.innerHTML

I'm looking for a way to search the generated source of a webpage ( document.innerHTML) for a string, in javascript.

I wouldn't want to use window.find() since i might have to look for id's or names too.

Any help would be appreciated.

Thanks

Upvotes: 2

Views: 30837

Answers (3)

Rakesh Chaudhari
Rakesh Chaudhari

Reputation: 3502

You could use an indexOf function to search string

var source = document.getElementsByTagName('html')[0].innerHTML;
var foundIndex = source.indexOf(searchString);

OR using regular expression,

var paragraph = 'The quick brown fox jumped over the lazy dog. It barked. ';
var regex = /quick/g;
var found = paragraph.match(regex);

ECMAScript 6

"hello".startsWith("ello", 1) // true
"hello".endsWith("hell", 4)   // true
"hello".includes("ell")       // true
"hello".includes("ell", 1)    // true
"hello".includes("ell", 2)    // false

Upvotes: 1

N 1.1
N 1.1

Reputation: 12524

document.innerHTML is undefined

var source = document.getElementsByTagName('html')[0].innerHTML;
var found = source.search("searchString");

Upvotes: 12

hallie
hallie

Reputation: 2845

try jQuery selector

Upvotes: 0

Related Questions