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/