Reputation: 143
I want to scan a passage and have any single letters changed (I have a dog that ate a chew toy)
A passage of text is placed in a textbox and then placed into a div. The modification can occur in either place I assume but I have had some trouble manipulating text in the div so far.
I am, however only able to use jquery/javascript/css/html because that's all I understand sadly.
Please explain the answer also as I often don't quite get the more complex of your answers but as always,
THANKS for ANSWERING!
Upvotes: 0
Views: 90
Reputation: 115222
Try something like this using replace()
$('div.text').html(function(i,v){
return v.replace(/(^|\s[A-Za-z]\s|$)/g,'<b>$&</b>');
});
Upvotes: 2
Reputation: 3268
You cand achive it with a regular expresion:
var text = "I have a dog that ate a chew toy";
text= " " + text + " ";
var matches = text.match(/\s\w\s/);
Explanation: you look this pattern: [space][letter][space], in concrete the case of first and last letter you don't have the first or last space, that's why I put two extra spaces on the text.
Upvotes: 1