0

I'm trying to replace a placeholder in a string I'm generating.

My string looks like this:

 var s = 'module("SlapOS UI Basic Interaction"); ' +
         'asyncTest( "${base_url}", function() { ' +
         ' expect( __number__ ); ' +
         ' ok(testForElement("div#global-panel"), "element present");' +
         ' start(); })';

And I want to replace __number__.

I can get the index correctly like so:

 s.indexOf("__number__");

but replacing does not work...

 s.replace("__number__", "1");

Question:
What am I doing wrong here? Makes no sense to my why it does not work.

4
  • 2
    s = s.replace("__number__", "1"); Commented Mar 28, 2014 at 16:48
  • 1
    Try RegExp .replace(/__number__/, "1"); Commented Mar 28, 2014 at 16:48
  • 1
    Works for me. Hate to ask this, but maybe you're forgetting to assign the result of the replace to something? Commented Mar 28, 2014 at 16:48
  • both don't work. No problem in Firebug though. Commented Mar 28, 2014 at 16:49

1 Answer 1

3

The replace method does not modify the existing string. It returns a new one.

var result = s.replace("__number__", "1");
Sign up to request clarification or add additional context in comments.

1 Comment

darn.... I think I always forget :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.