admin管理员组文章数量:1314565
I am having an issue which I cannot find documented anywhere, i see a regex method however that is using a direct string rather than string within a variable. This is my code:
var src = getQueryVariable("srcStr");
if(src != false){
$(".entry-content").html($(".entry-content")
.html().replace(/src/g, "<span style='background-color:#f2e128;'>"
+ src + "</span>"));
}
This gets a url variable (srcStr) and searches a body of text within .entry-content for the string stored in the var src
.
The problem code is here: replace(/src/g
Is there a solution?
I am having an issue which I cannot find documented anywhere, i see a regex method however that is using a direct string rather than string within a variable. This is my code:
var src = getQueryVariable("srcStr");
if(src != false){
$(".entry-content").html($(".entry-content")
.html().replace(/src/g, "<span style='background-color:#f2e128;'>"
+ src + "</span>"));
}
This gets a url variable (srcStr) and searches a body of text within .entry-content for the string stored in the var src
.
The problem code is here: replace(/src/g
Is there a solution?
Share Improve this question asked Dec 3, 2012 at 12:48 Josh BootheJosh Boothe 1,4234 gold badges25 silver badges41 bronze badges 1- I might not understand what you're trying to say, but anytime you see a method that asks for a string, you can use a variable that's storing a string. – Gonzalo Quero Commented Dec 3, 2012 at 12:51
1 Answer
Reset to default 9You are searching for the pattern that is literally "src." You need to use the RegExp
class if you want to use variables in patterns:
pattern = new RegExp(src, 'g');
$(".entry-content")...html().replace(pattern, replacement);
本文标签: jqueryReplace all instances in string with a variable as the searchJavaScriptStack Overflow
版权声明:本文标题:jquery - Replace all instances in string with a variable as the search - JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741965997a2407551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论