adnan kamili
adnan kamili

Reputation: 9445

Javascript regex- How to match xpaths

I am creating a regex for matching xpaths generated by firebug, cna some one help me with that, an example xpath is:

.//*[@id='tab-HOME']/li[2]/span/span[1]/span/span[2]/span[2]/span/span
.//*[@id='any_possible_id']/span/span[2]/span/span

Now keeping in mind the names alowed for id's in javascript what can be the possible regex. I want to match

.//*[@id='any_possible_id']/li

Here is what I tried:

alert(/^\.\/\/\*\[[@id=]*\]/.test(xpath));

certainly incomplete.

Upvotes: 0

Views: 49

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Use [^\]]+ after the id to match until the next ] of the [@id=...]

alert(/^\.\/\/\*\[@id=[^\]]+\]\/li/.test(xpath));

If you do not want to match only for ../li then remove the li from the regex.

Upvotes: 1

Related Questions