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 <>.

1

4 Answers 4

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/

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

3 Comments

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.
@adn295 See the last example for multiple matches. You can save the values in the "name" "surname" variables.
@adn295 Changed the example (the last one). Like that?
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.

Comments

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

Comments

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

results in I have Any Thing

2 Comments

Thanks for reply... But there may be anything instead of name. It should also be replicable. How to do that ???
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.

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.