Javascript replace words from a string - javascript

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

Related

How to split string from particular word in javascript?

[
{
docPath: "c:\Project\Indofebweb\Attachment\images\indofab.png",
}
]
I want to split the string from \Attachment and get the rest of the string e.g
\Attachment\images\indofab.png . How can I do this?
For this particular string you can use :
var arr=test.split("Indofebweb");
var restString=arr[1]; //-----contains string \Attachment\images\indofab.png
You can make your own logic using split() function.
UPDATED CODE----TRY THIS----BELOW HTML
<!DOCTYPE html>
<html>
<body>
<p> a string display :</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "c:\\Project\\Indofebweb\\Attachment\\images\\indofab.png";;
var arr=str.split("Indofebweb");
document.getElementById("demo").innerHTML = arr[1];
}
</script>
</body>
</html>
use str.match()
path.match(/\\Attachment.*/);
this will return everything after and including "\Attachment". Keep in mind that backslashes need to be escaped.

How to replace "#[" anywhere in the string

My question is about regular expressions. I want to replace "#[" anywhere in the string. Tried with [#[] expression, but it's not working, because it replaces # anywhere in the string.
you can use the regex
/#\[/g
function replace(str){
return str.replace(/#\[/g, '');
}
console.log(replace('#[]'));
console.log(replace('abcd#[absd]ahah#[ahah'));
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">Visit Microsoft! #[</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/#\[/g, "W3Schools");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Simple Javascript on html textbox conversion. Doesn't work twice

I'm using the replace method and if I type in "test test" only the first test gets converted to good so it'll become "good test". I'm at a loss on why this is happening. On a side question, if I was to add 20 other words that I would like to replace, would I have to create 20 different str.replace?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>
<script>
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace("test", "good");
document.getElementById("secondbox").value = res;
}
</script>
</body>
</html>
Any help would be greatly appreciated
Use regex, change "good" to /good/g
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace(/test/g, "good");
document.getElementById("secondbox").value = res;
}
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>

Javascript - Replace html using innerHTML

I'm trying to replace html using innerHTML javascript.
From:
aaaaaa/cat/bbbbbb
To:
Helloworld
This's my code
<html>
<head>
</head>
<body>
<p id="element1">aaaaaa/cat/bbbbbb</p>
<script language="javascript">
var strMessage1 = document.getElementById("element1") ;
strMessage1.innerHTML = strMessage1.innerHTML.replace( /aaaaaa./g,'<a href=\"http://www.google.com/') ;
strMessage1.innerHTML = strMessage1.innerHTML.replace( /.bbbbbb/g,'/world\">Helloworld</a>') ;
</script>
</body>
</html>
When i run this code it disappears Helloworld hyperlink.
what I'm doing wrong. Please help.
Thank you for all your help.
You should chain the replace() together instead of assigning the result and replacing again.
var strMessage1 = document.getElementById("element1") ;
strMessage1.innerHTML = strMessage1.innerHTML
.replace(/aaaaaa./g,'<a href=\"http://www.google.com/')
.replace(/.bbbbbb/g,'/world\">Helloworld</a>');
See DEMO.
You are replacing the starting tag and then putting that back in innerHTML, so the code will be invalid. Make all the replacements before you put the code back in the element:
var html = strMessage1.innerHTML;
html = html.replace( /aaaaaa./g,'<a href=\"http://www.google.com/');
html = html.replace( /.bbbbbb/g,'/world\">Helloworld</a>');
strMessage1.innerHTML = html;

Javascript String.replace() ,ambiguous results

See my code
<html>
<body>
<script type="text/javascript">
var str="Visit Microsoft!";
document.write( str = str.replace("",'ss'));
</script>
</body>
</html>
The output is
ssVisit Microsoft!
Why is it happening.?
This is correct because every string begins with an empty string. See below post for more info:
Why does "abcd".StartsWith("") return true?

Categories