如何使用javascript和jQuery进行复杂的替换?
在我的网页上,我有如下来源:如何使用javascript和jQuery进行复杂的替换?,javascript,jquery,Javascript,Jquery,在我的网页上,我有如下来源: <div id="opt_3" >A)</div> <div id="opt_2" >B)</div> <div id="opt_4" >C)</div> <div id="opt_5" >D)</div> <div id="opt_1" >E)</div> function textReplace(v){ return v.r
<div id="opt_3" >A)</div>
<div id="opt_2" >B)</div>
<div id="opt_4" >C)</div>
<div id="opt_5" >D)</div>
<div id="opt_1" >E)</div>
function textReplace(v){
return v.replace(/text ([0-9])/gi,function(i,e){
return "<strong>"+$("#opt_"+e).text().substring(0,1)+"</strong>";
});
}
并将其更改为
<strong>E</strong> word word word this is a <strong>A</strong> word word.
abc <strong>C</strong> word
并将其更改为
<strong>E</strong> word word word this is a <strong>A</strong> word word.
abc <strong>C</strong> word
abcCword
我的javascript的任务是获取字符串“Text X”或“Text X”中的数字,查看id字段中与X值匹配的第一个字符,并将该字符替换为“Text X”
我已加载jQuery。能给我一些建议吗?我需要使用javascript和jQuery吗
有谁能给我一些建议,我该怎么做。我不太熟悉javascript或jQuery。您应该这样做
var myText = 'Lorem {0} Dolor {1} Amet is a {2} text.';
var textReplace = function(txt) {
for (var i = 0, ii = arguments.length - 1; i < ii; i++) {
txt = txt.replace('{' + i + '}', arguments[i + 1]);
}
return txt;
}
textReplace(myText, 'ipsum', 'sit', 'dummy');
var myText='Lorem{0}Dolor{1}Amet是一个{2}文本';
var textReplace=函数(txt){
for(var i=0,ii=arguments.length-1;i我希望这会有所帮助。以下代码应该完全按照您的描述执行:
var input = "Text 1 word word word this is a text 3 word word.";
input = input.toLowerCase();
var split = input.split(" ");
for(var i = 0; i < split.length; i++) {
if(split[i].toLowerCase() == "text") {
var num = split[i+1];
var val = $("#opt_" + num).text();
input = input.replace("text " + num, "<strong>" + val + "</strong>");
}
}
alert(input);
var input=“Text 1单词这是一个Text 3单词。”;
input=input.toLowerCase();
var split=input.split(“”);
对于(变量i=0;i“+val+””;
}
}
警报(输入);
你可以看到它在工作。它在空格上拆分字符串,然后在生成的数组中循环,查找单词“text”的出现。当它找到一个单词时,它会用相应元素的值替换该单词和下一个单词(根据您的说明,这将是一个数字),并用
strong
标记进行包装。以下代码应该完全按照您描述的方式执行:
var input = "Text 1 word word word this is a text 3 word word.";
input = input.toLowerCase();
var split = input.split(" ");
for(var i = 0; i < split.length; i++) {
if(split[i].toLowerCase() == "text") {
var num = split[i+1];
var val = $("#opt_" + num).text();
input = input.replace("text " + num, "<strong>" + val + "</strong>");
}
}
alert(input);
var input=“Text 1单词这是一个Text 3单词。”;
input=input.toLowerCase();
var split=input.split(“”);
对于(变量i=0;i“+val+””;
}
}
警报(输入);
你可以看到它在工作。它在空格上拆分字符串,然后在生成的数组中循环,查找单词“text”的出现。当它找到一个单词时,它会用相应元素的值替换该单词和下一个单词(根据您的说明,这将是一个数字),并用
strong
标记进行包装。为此,您需要jQuery
//get the original text
txt = $('#text-container').text();
//split the whole text by the words "Text"
// this way each string in the array arr will contain as its first member the number
// without the splitting text
arr = txt.split('Text');
answerText = '';
for(i=0; i < arr.size(); i++){
words=arr[i].split(' '); //split by spaces
//the first word will be the number
nr = words[0];
//then we look up the corresponding option, and substitute the number
words[0] = $('#opt_'+nr).text();
//and rebuild the original text
answerText += words.join(' ');
}
//获取原始文本
txt=$(“#文本容器”).text();
//将全文拆分为“文本”
//这样,数组arr中的每个字符串将包含数字作为其第一个成员
//没有拆分文本
arr=txt.split('Text');
答案文本=“”;
对于(i=0;i
为此,您需要jQuery
//get the original text
txt = $('#text-container').text();
//split the whole text by the words "Text"
// this way each string in the array arr will contain as its first member the number
// without the splitting text
arr = txt.split('Text');
answerText = '';
for(i=0; i < arr.size(); i++){
words=arr[i].split(' '); //split by spaces
//the first word will be the number
nr = words[0];
//then we look up the corresponding option, and substitute the number
words[0] = $('#opt_'+nr).text();
//and rebuild the original text
answerText += words.join(' ');
}
//获取原始文本
txt=$(“#文本容器”).text();
//将全文拆分为“文本”
//这样,数组arr中的每个字符串将包含数字作为其第一个成员
//没有拆分文本
arr=txt.split('Text');
答案文本=“”;
对于(i=0;i
您可以使用回调函数替换不区分大小写的RegExp,如下所示:
<div id="opt_3" >A)</div>
<div id="opt_2" >B)</div>
<div id="opt_4" >C)</div>
<div id="opt_5" >D)</div>
<div id="opt_1" >E)</div>
function textReplace(v){
return v.replace(/text ([0-9])/gi,function(i,e){
return "<strong>"+$("#opt_"+e).text().substring(0,1)+"</strong>";
});
}
功能文本替换(v){
返回v.replace(/text([0-9])/gi,函数(i,e){
返回“”+$(“#opt"+e).text()。子字符串(0,1)+“”;
});
}
示例:您可以使用回调函数替换不区分大小写的RegExp,如下所示:
<div id="opt_3" >A)</div>
<div id="opt_2" >B)</div>
<div id="opt_4" >C)</div>
<div id="opt_5" >D)</div>
<div id="opt_1" >E)</div>
function textReplace(v){
return v.replace(/text ([0-9])/gi,function(i,e){
return "<strong>"+$("#opt_"+e).text().substring(0,1)+"</strong>";
});
}
功能文本替换(v){
返回v.replace(/text([0-9])/gi,函数(i,e){
返回“”+$(“#opt"+e).text()。子字符串(0,1)+“”;
});
}
示例:这不是一个答案,只是一个注释:JQuery是一个用Javascript编写的库。因此,如果您使用的是JQuery,那么您使用的是Javascript。@Jason-谢谢您的评论。是的,也许我的问题措辞不太好。理想情况下,我希望在jQuery中使用所有内容,但我认为这要求太高了,尽管jQuery似乎可以做很多事情。正如Fatih所建议的,如果可以,您应该使用“文本X”以外的其他内容。比如一个标签。然后,选择和更改样式将非常容易。这不是一个答案,只是一个注释:JQuery是一个用Javascript编写的库。因此,如果您使用的是JQuery,那么您使用的是Javascript。@Jason-谢谢您的评论。是的,也许我的问题措辞不太好。理想情况下,我想在j