ããã以ä¸ã®ããã« XPath ã«æååãåãè¾¼ãäºãããã¾ã
document.evaluate('//*[@class="' + text + '"]', document, null, 7, null);
ã¾ããåããããããªã³ã¼ãæ¸ããã§ããã©ã
ã§ããããã£ã¦
text ãå¤é¨ããæ¥ããã®ã ã£ãããæå³éãã®åä½ãããªããã§ããã
ãã¨ãã°ã以ä¸ã®ãããªä¾ã§ãã
var text = '"] | /hoge/fuga/piyo | .["'; document.evaluate('//*[@class="' + text + '"]', document, null, 7, null);
ã¨ããããã§
ä»»æã®æååã XPath ã®å¼ã«å¤æãã JavaScript ãæ¸ãã¦ã¿ã
以ä¸ã§è©¦ãã¾ã
http://amachang.sakura.ne.jp/misc/xpath_escape/ âã¯ãªãã¯ï¼
ã³ã¼ãã¯ãã¡ã
function escapeXPathExpr(text) { var matches = text.match(/[^"]+|"/g); function esc(t) { return t == '"' ? ('\'' + t + '\'') : ('"' + t + '"'); } if (matches) { if (matches.length == 1) { return esc(matches[0]); } else { var results = []; for (var i = 0; i < matches.length; i ++) { results.push(esc(matches[i])); } return 'concat(' + results.join(', ') + ')'; } } else { return '""'; } }
以ä¸ã®ããã«ä½¿ãã¾ã
document.evaluate('//*[@class=' + escapeXPathExpr(text) + ']', document, null, 7, null);
ã¾ã¨ã
æ¥ãã§ã³ã¼ããæ¸ãã¦ããã¨ãã¯å¿ããã¡ã§ããã
ãæèãã¨ãä»»æã®ãã¼ã¿ããããã°ããã£ã±ãããã«ã¯ã¨ã¹ã±ã¼ãã®å¿
è¦æ§ãããã¨ãããã¨ãå¿ãã¦ã¯ããã¾ããã