1

I am working with javascript and I have created a jsfiddle.

I have two textfields: one contains user input and the other one contains result. My problem is when I enter the same word twice then only one of them gets replaced.

For example, if I enter "going,going" in the first text field then the result is "GNG,going" - instead of "GNG,GNG". What am I doing wrong in my code?

HTML:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Scrolling DIV demo on iPhone / iPod Touch / Android / iPad</title>
</head>
<body>
    <input type="text" id="first_text" value="going, used to, to do fast, as soon as possible"/>
    <input type="text" id="second_text"/>
    <input type="button" onclick="replaceText()" value="Click!"/>
</body>
</html>

Javascript:

var replaceText = function () {
    var inputval = document.getElementById('first_text').value;
    var arr = {
        "going": "GNG",
        "used to": "UD",
        "as soon as possible": "ASAP",
        "to do fast": "tdf"
    }

    for(var key in arr) {
        if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
    }
    document.getElementById("second_text").value = inputval;
}
2
  • 2
    I would like to note that all of the answers assume your keys do not contain special characters (which is the case here). If you do add special characters, then you would need to escape them with \\ before the characters. Commented Nov 29, 2011 at 13:35
  • This question is similar to: How do I replace all occurrences of a string in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 6, 2024 at 7:45

4 Answers 4

5

use inputval.replace(RegExp(key,"g"), arr[key]) in other words, create a Regular Expression from you key value add the 'g[lobal]' modifier to it.

Sign up to request clarification or add additional context in comments.

Comments

3

Change:

inputval = inputval.replace(key, arr[key])

To:

inputval = inputval.replace(new RegExp(key, 'g'), arr[key])

The replace function and regular expressions are not global, by default, that's what the g switch does. Working example here: http://jsfiddle.net/NSy8P/1/

Comments

1

thats because replace just does the operation on the first occurence instead you can use the following :

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

http://naspinski.net/post/Javascript-replaceAll-function.aspx

Comments

1

Replace only replaces the first occurence of the pattern. If you want to replace more then once you will need to use the regexp version of the code. (note the "g" standing for "global replace")

'going, going'.replace(/going/g, 'Go'); // 'Go, Go'

You can dinamically create a regexp from a string if you still want to keep the same code (instead of rewriting it to use regexp patterns directly)

var pattern = new RegExp('going', 'g');

But then you must not have regexp special chars in your pattern. If you want to be able to use special characters, like parenthesis or backslashes, in the key you will need an escape function, like the following one:

function regex_escape(str){
    return str.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1');
}

var pattern = new RegExp (regex_escape(key), 'g');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.