简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM