Clarinetking
Clarinetking

Reputation: 143

How to detect single letters and modify their look/css

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

Answers (2)

Pranav C Balan
Pranav C Balan

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

DEMO

Upvotes: 2

Manjar
Manjar

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

Related Questions