Javascript replace method review

Post on February 7th, 2012 @ 1:07 AM by |Posted in : javascript code and samples | Tagged in : | 1 Comment |Send Email

This morning when I want to write code for my plugins I have run into this problem—>How to replace a sub string within a text with another sub string using javascript. Even though I end up not using that javascript method but I would like to show you how to do it anyway in this article.

Suppose we have a string ‘Hello World’ and we would like to replace the entire word ‘World’ within that string with ‘Dude’ and then assign the new string to a new variable, we will use the replace method to do the job for us.


<script>
var greet = "Hello World";
var greetDude = greet.replace("World", "Dude");
</script>

Suppose you would like to replace the sub string ‘him’ within a text with another sub string from the user input and then displaying the latest changes on the alert box, you can do it with below code.

<script>
var text = "You will replace 'him' with new word, "
  + "so go ahead and type any word into the prompt box now!";
window.alert("You will replace him with new word, "
  + "so go ahead and type any word into the prompt box now!");
var repText = window.prompt("Enter the word to replace him","");
window.alert(text.replace(/him/g, repText));
</script>

Do you also know that we can create a new RegExp object instance within the replace method like this?

new RegExp(pattern, flags);

For example if we want to create a new text string by replacing the old word ‘this’ with the new word ‘my’, then we can use the following code.


<script>
var oldText = "hello this world";
var newText = oldText.replace(new RegExp("this","g"),"my");
</script>

In the next example, we are going to use the inline function to replace every number ’1′ within a string with the word ‘one’. As you can see, the replacement function will accept the matched snippets as its parameter and replacing them with the word ‘one’, the whole string is then return and appears on the alert box.

<script>
function numberToWord(stringValue) {  
  function changeNumberToWord(match) {  
    return 'one';
  }  
  return stringValue.replace(/1/g, changeNumberToWord);  
}
window.alert(numberToWord('1 + 1'));
</script>


continue on : How to call a javascript function when someone is clicking on a button of your website

This post will show you how to create a button which will call the javascript function when someone clicking on it – full code provided!

Read More

Return the length of a string with javascript program

This tutorial will show you how to return the length of a string with javascript program – source code provided

Read More

How to use ‘this’ in javascript

Tutorial showing you how to use ‘this’ in javascript

Read More

How to create a website redirect link with javascript

Sample javascript code for creating a redirect link for your website.

Read More

How to access the name property of a javascript function

Example showing you how to output the name of a function in javascript

Read More

Changing javascript array element

Example with source code showing you how to change the array elements in javascript

Read More

How to use the in javascript statement?

Tutorial showing in javascript statement with code fully provided

Read More

javascript for statement

javascript for statement in action with code and sample provided.

Read More

javascript for in

javascript for in statement showing the key and value pairs of an object. javascript for in statement showing the properties of window object also with free javascript code provided.

Read More

How to create a new string object instance with javascript program

Example showing you how to create a new instance of the string object with javascript – code provided!

Read More

Javascript prototype in action

In this article you will learn how to use javascript prototype by creating a simple program which will show an alert box with ‘Hello World!’ text.

Read More

Change string to upper case with Javascript

This article will show you how to change the string to upper case with javascript,

Read More

Change javascript array elements

This article will show you how to change the javascript array elements.

Read More


One comment on “Javascript replace method review

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>