Context in function called by string replace
by leemeador
HTML
<div id="fails">
<p>This is the result:</p>
</div>
<hr>
<div id="works">
<p>This is the result:</p>
</div>
JavaScript
$("div#fails").append(
{
worldString : "World",
myFunction: function() {
var msg = "Hello name! Hello nime! Hello nouiame!";
var result = msg.replace(/n[aeiou]+me/g, function(matched) {
return this.worldString + " (" + matched.split("").reverse().join("") + ")";
});
return result;
}
}.myFunction()
);
$("div#works").append(
{
worldString : "World",
myFunction: function() {
var self = this;
var msg = "Hello name! Hello nime! Hello nouiame!";
var result = msg.replace(/n[aeiou]+me/g, function(matched) {
return self.worldString + " (" + matched.split("").reverse().join("") + ")";
});
return result;
}
}.myFunction()
);