I have this text in javascript:
'This is a %{component} with a %{value}'
I should replace all words that look like %{\w+}
with <span>word</span>
The final result should look like:
'This is a <span>component</span> with a <span>value</span>'
I have this text in javascript:
'This is a %{component} with a %{value}'
I should replace all words that look like %{\w+}
with <span>word</span>
The final result should look like:
'This is a <span>component</span> with a <span>value</span>'
As already written use String.replace
and a RegEx:
var originalString = 'This is a %{component} with a %{value}';
var replacedString = originalString.replace(/%\{(\w+)\}/g, '<span>$1</span>');
// "This is a <span>component</span> with a <span>value</span>"
Is this what you are looking for?
<!DOCTYPE html>
<html>
<body>
<p id="demo">This is a %{component} with a %{value}</p>
<button onclick="myFunction()">Test</button>
<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML;
var n=str.replace("%{","< span >").replace("}","< /span >");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
You can do that with regex in javascript / jquery. Have a look here: http://de.selfhtml.org/javascript/objekte/regexp.htm