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>

“ From the outset, invent Partners got the balance right and were prepared to listen to our needs, whilst at the same time offering their depth of knowledge and experience. The end product we received is exceptional and has since proven its worth, fundamentally from a ‘back end’ input and management information viewpoint and importantly from a ‘front end’ consumer ease of use stance. Needless to say, we are growing our business as a result, with 80% of web business as ‘net new’. ”
Ged, Vertigrow Ltd

Contact Us

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