私はjavascriptでこのテキストを持っています:
'This is a %{component} with a %{value}'
次のような単語をすべて置き換える必要があり%{\w+}
ます<span>word</span>
最終結果は次のようになります。
'This is a <span>component</span> with a <span>value</span>'
私はjavascriptでこのテキストを持っています:
'This is a %{component} with a %{value}'
次のような単語をすべて置き換える必要があり%{\w+}
ます<span>word</span>
最終結果は次のようになります。
'This is a <span>component</span> with a <span>value</span>'
すでに書かれているようString.replace
に、使用と正規表現:
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>"
これはあなたが探しているものですか?
<!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>
javascript/jqueryの正規表現でそれを行うことができます。こちらをご覧ください:http://de.selfhtml.org/javascript/objekte/regexp.htm