简体   繁体   English

Javascript正则表达式-如何匹配xpath

[英]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: 我正在创建一个正则表达式来匹配由萤火虫生成的xpath,cna可以帮助我解决这个问题,示例xpath是:

.//*[@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. 现在记住在javascript中ID的名称可能是正则表达式。 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=...] 使用[^\\]]+后ID匹配,直到下一个][@id=...]

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

If you do not want to match only for ../li then remove the li from the regex. 如果您不想只匹配../li ,请从正则表达式中删除li

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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