Javascript String Replace Example

Replacing strings with javascript

Demo

I'm @little te@pot

Output goes here

Replace Text

Code

<p id="source">I'm @little te@pot</p>
<p id="output">Output goes here</p>
<p><a href="#" onClick="return replaceText()">Replace Text</a></p>
<script type="text/javascript">
function replaceText(){
    sourcetext = document.getElementById('source').innerHTML;


    // The string replace function
    outputtext = sourcetext.replace(/@/gi, "REPLACEMENT");


    document.getElementById('output').innerHTML = outputtext;
    return false;
}
</script>

“ Just wanted to send a quick 'thank you' to the Invent Partners team for all your help and support. I love the design of our new site and really appreciate the great service you provided at every stage - you turned a daunting project into one that I really enjoyed! Thanks again. ”
Lou, Advanced Writing Solutions

Contact Us

© 2025 Invent Partners Ltd | Supporters of FdA Web Design, Wakefield