News: JavaScript replace Function

Home News Photos News Videos News Cartoons News Blogs RSS

Home > Java Scripts Examples and Tutorials > JavaScript replace Function

JavaScript replace Function

The function replace can be used to replace a string with another string. A regular expression can be used to find the string.

Wednesday, Oct 11, 2006 | 1726 Views | Comments [View/Post]

<html>

<head>

<script type="text/javascript">

            var str = new String("Hello World")

            document.write(str+"<br/>")

            var regEx = new RegExp ('World', 'gi') ;

            str = str.replace(regEx, 'User')

            document.write(str)

</script>

</head>

<body></body>

</html>

Output

Hello World

Hello User


Comment on this article
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
News Other Language
World News
Movie News
Information
Travel News