KJW
KJW

Reputation: 15251

Javascript: how to xpath string at a[integer]?

I have an xpath string like the following:

/html/body/table/a/img
/html/body/table/a[3]/img

Sometimes there is [integer], other times there isn't.

I would like a regex to split a string which works with "a" or "a[integer]".

string.split(/a[0-9]/);

Upvotes: 0

Views: 203

Answers (1)

user166390
user166390

Reputation:

I would highly recommend an xpath-aware library (some browsers support evaluate/XPath support from JS), but.. perhaps:

/\/a(?:\[\d+])?\//

That will match "/a/" and "/a[1234]/" (but not "a[]" or even "a/"), etc. Note that I included the "/" into the match so as to not pick up false-positives too easily -- wouldn't want to match "t*a*ble".

Upvotes: 1

Related Questions