[英]Why this doesn't work?
var str="Hello World!";
var patt=/\x57/g;
var matched = str.match(patt);
document.write(matched.strike());
似乎问题是matched.strike()
。 只有matched
它才有效。 所以为什么?
.strike()
方法对字符串进行操作, matched
是一个数组。 您必须取消引用第一个匹配才能看到正确的输出。
var str="Hello World!";
var patt=/\x57/g;
var matched = str.match(patt);
// Access first element of the matched array
document.write(matched[0].strike());
// Or via .pop() or .shift()
document.write(matched.pop().strike());
// or
document.write(matched.shift().strike());
// either returns "<strike>W</strike>"
String.strike()
方法是非标准的 ,不应该依赖于完整的浏览器支持。
(非常旧的) strike
方法是String
方法。 RegExp.match
返回一个不知道strike
方法的Array
。
您的代码可以重写为:
var str = 'Hello World'.replace(/\x57/g,function(s){return s.strike();});
//=> Hello <strike>W</strike>orld
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.