开发者

How can I do a complex substitution that using javascript and jQuery?

On my web pages I have source that looks like this:

<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>

What I need is to create a javascript that when running takes something like this as an input var:

Text 1 word word word this is a text 3 word word.

and changes it to

<strong>E</strong> word word word this is a <strong>A</strong> word word.

or

abc text 4 word 

and changes it to

abc <strong>C</strong> word

The job of my javascript will be to taken the number inside of the string "Text X" 开发者_如何转开发or "text X", look at the first character of the id field that matches the value of X and substitute that character for "Text X".

I have jQuery loaded. Can that help by giving me some suggestioins? Do I need to use javascript as well as jQuery?

Can someone give me some advice on how I could do this. I am not very familiar with javascript or with jQuery.


You should this

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');

This function requires arguments. The first one is the text that you want to replace in a way. Other arguments will be replaced in your text. I suggest you to use wrapper texts with curly braces instead Text 4 or whatever.

I hope this will help.


The following code should do exactly as you describe:

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);

You can see it working here. It splits your string on spaces, then loops through the resulting array, looking for occurences of the word "text". When it finds one, it replaces that word and the next word (which will be a number according to your instructions) with the value of the corresponding element, wrapped in strong tags.


You could do a case-insensitive RegExp replace with a callback function, like this:

function textReplace(v){
    return v.replace(/text ([0-9])/gi,function(i,e){
        return "<strong>"+$("#opt_"+e).text().substring(0,1)+"</strong>";
    });
}

example: http://jsfiddle.net/niklasvh/pLpxN/


For this you will need 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(' ');
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜