0

I have a problem to add the dynamic values to a string.

For example:

My name is < NAME >

Now I want to replace NAME with my name so my string will become:

My name is anything.

How I can achieve this?

The dynamic part will be always between <>.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
Ved
  • 8,577
  • 7
  • 36
  • 69
  • possible duplicate of [Javascript multiple replace](http://stackoverflow.com/questions/832257/javascript-multiple-replace) – Yoshi Oct 19 '11 at 09:53

4 Answers4

2
var result = "My name is < NAME >".replace("< NAME >", "anything");

do you want this? Or are you searching for something more complex that enums all the words between < and >? Something like:

var matches = "My name is < NAME > < SURNAME >".match(/<[^>]+>/g);

If you want to replace multiple parts you could do this:

var replacer = function (str)  
{  
    if (str === '< NAME >')
    {
        return 'anything';
    }
    else
    {
        return 'nothing';
    }
};

var replaced = 'My name is < NAME > < SURNAME >'.replace(/<[^>]+>/g, replacer));

Test here:

http://jsfiddle.net/rZuNX/

A more complete example:

var name = 'anything';
var surname = 'anyone';

var myInputText = "My name is < NAME > < SURNAME >";

var replaced = myInputText
    .replace(/<[^>]+>/g, 
        function (str)  
        {  
            if (str === '< NAME >')
            {
                return name;
            }
            else if (str === '< SURNAME >')
            {
                return surname;
            }
            else
            {
                return '***error***';
            }
        }
    );

I'm using javascript anonymous functions that "close" around the name and surname variables.

Test it here: http://jsfiddle.net/rZuNX/1/

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Thanks for reply I want that anything between "< >" should be replaced by another value that I will give. It may be name, city etc.. Also string is not known to me except I know that I should replace the text between "<>" that is it. – Ved Oct 19 '11 at 10:06
  • @adn295 See the last example for multiple matches. You can save the values in the "name" "surname" variables. – xanatos Oct 19 '11 at 10:07
  • 1
    @adn295 Changed the example (the last one). Like that? – xanatos Oct 19 '11 at 10:11
0
var str = "My name is < NAME >";
 str = str.replace("NAME", "anything");
0

/< NAME >/.replace("My name is < NAME >", "anything") is the basic form for a regualr expression, obviously you can then begin to manipulate the regexp to match other things. W3Schools is a good starting point on RegExp in Javascript, and this online tester is very good.

Steve
  • 1,440
  • 13
  • 13
-1
"I have <name>".replace(/<name>/, 'Any Thing')

results in I have Any Thing

dmitry
  • 4,989
  • 5
  • 48
  • 72
  • Thanks for reply... But there may be anything instead of name. It should also be replicable. How to do that ??? – Ved Oct 19 '11 at 09:54
  • 1
    please, look at that link in comment below the question. For multiple replaces use /g modifier, and just loop through your subsitutions and do the thing in a loop. Of course, that will be better to wrap functionality into class. – dmitry Oct 19 '11 at 09:59