How to control the font weight of any text in a paragraph (<p> tag) in curly braces or curly braces {} using JavaScript
I would like to be able to control the font-weight of the text if it is squared inside the p tag using JavaScript.
For example: A cow jumped over the moon. The font-weight inside {} will be increased.
This means that the end user can enter this into the text area and send a print page to a page that changes the font in brackets or curly brackets.
Any help on this would be great.
Here's how you can do it:
var ps = document.getElementsByTagName('p'); foreach = Array.prototype.forEach; foreach.call(ps, function (p) { var content = p.innerHTML; p.innerHTML = content.replace(/\{(.*?)\}|\((.*?)\)/g, function (m) { return '<span style="font-weight: bold;">' + m + '</span>'; }); }); โ And of course fiddle . For example, you only need pure JavaScript, no additional libraries.
- Edit:
If you do not want to see parentheses as a result, you can use:
var ps = document.getElementsByTagName('p'); foreach = Array.prototype.forEach; foreach.call(ps, function (p) { var content = p.innerHTML; p.innerHTML = content.replace(/\((.*?)\)|\{(.*?)\}/g, function (m) { return '<span style="font-weight: bold;">' + m.replace(/[\(\)\{\}]/g, '') + '</span>'; }); }); Fiddle: http://jsfiddle.net/ma47D/4/
Best wishes!
You can do this with mootools as follows:
window.addEvent('domready', function() { $$('P').each(function(p) { p.set('html', p.get('text').replace(/{([^\}]*)*}/g,"<b>$1</b>")); }); });โ domready is important because it has to be done after the page has fully loaded. converting to jquery would not be so difficult.
Locally, you can handle it like this:
<!DOCTYPE html> <html> <head> <script> function transfer(){ document.getElementById("result").innerHTML=document.getElementById("demo").value.replace(/{/g,'<strong>').replace(/}/g,'</strong>'); } </script> </head> <body> Input: <input type="text" name="input" id="demo"><br> <input type="button" value="Submit" onclick="transfer();"> <p id="result"></p> </body> </html> If you send text to the server, magic can be performed similarly on the server side.
My suggestion
<!DOCTYPE html> <html> <head> <style> p span { font-size:1.5em; } </style> <script> function regex(){ document.getElementById("output").innerHTML= document.getElementById("input").value.replace(/{(.*?)}/g, "<span>$1</span>"); }; </script> </head> <body> <p id="output"></p> <textarea id="input" rows="30" cols="80"></textarea> <input type="button" value="Input" onclick="regex();"/> </body> <html> Of course, before sending you need to misinform your data.
If you tried something, but I'm sure there are more elegant solutions.
$(document).ready(function(){ $(tb).blur(function(){ var str = ''; var nextFont = 0; $.each($(tb).val(),function(i,char){ if(nextFont == 0){ if(char == '{'){ if($(tb).val().indexOf(i,'}')){ str += '<font size="15">'; nextFont = $(tb).val().indexOf('}', i); } else { str += char; } } else { str += char; } } else if (nextFont === i) { str += '</font>'; nextFont = 0; } else { str += char; } }); $("#txt").html(str); }); });