Closed alecthomas closed 7 months ago
There are two problem.
replace()
not supportd the regex syntax.replace()
as a function and return a value with string type, you should call xpath.Evaluate(...)
, the htmlquery.QueryAll()
always return a set of node.expr, err := xpath.Compile(`replace((//a[contains(@href, '/stringer-')])/@href, '^.*/stringer-v([^-]*)-.*$', '$1')`)
if err != nil {
panic(err)
}
v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc))
fmt.Println(v.(string))
Thank you for the pointers, I've switched to using substring-after
and substring-before
. BTW it looks like replace should support regex?
Thanks for a great set of libraries BTW, really nicely done.
On playground: https://go.dev/play/p/jxU6UgH0DnK The same content+query works fine on https://www.freeformatter.com/xpath-tester.html
The above example without
replace()
works fine: https://go.dev/play/p/N22KULbkgRu