function areaMe(area) {
var barea = $('#barea').val();
if (barea.indexOf(area) != -1) {
alert ("..." + barea + "..." + area + "...");
barea.replace(area, "cu"); // Remove
alert ("..." + barea + "..." + area + "...");
}
else {
barea += area + ' '; // Include.
}
$('#barea').val(barea);
}
The "replace is not a function" error occurs when we call the replace() method on a value that is not of type string . To solve the error, convert the value to a string using the toString() method before calling the replace() method.
To perform a global search and replace, use a regular expression with the g flag, or use replaceAll() instead. If pattern is an object with a Symbol. replace method (including RegExp objects), that method is called with the target string and replacement as arguments.
The replaceAll() method will substitute all instances of the string or regular expression pattern you specify, whereas the replace() method will replace only the first occurrence.
To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d. *\d/ , so it is replaced.
barea = barea.replace(area, "cu")
You need to assign it since String.prototype.replace
isn't a mutator method.
You need to assign the replaced value back to your variable:
barea = barea.replace(area, "cu");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With