I wrote the following function in javascript:
function maskString(input) {
return input.replace(/\s\s+/," ");
}
Very simple.
In a second function I wrote:
function secondFunction(string1, string2) { //Attetion! See Update 2!
var string1masked = maskString(string1);
var string2masked = maskString(string2);
( ... )
}
The error message is:
TypeError: Result of expression 'input.replace' [undefined] is not a function.
Anybody an idea? Google wasn’t really helpful :\
UPDATE 1:
I’m using jQuery and string1 is from an textbox. I’m calling the second function like this:
var bool = secondFunction (textarea1.val(), textarea2.val()); //Attetion! See Update 2!
UPDATE 2:
I was wrong with the second function … It’s:
function secondFunction(string1, array1) {
var string1masked = maskString(string1);
var array1masked = maskString(array1);
( ... )
}
So my function doesn’t work with the array. Unfortunately I have no idea how to change it 🙁
I guess you have code like this:
textarea1.val()returnsundefinedif the element is not found (i.e. an element with IDtextarea1). Check if you haven’t made a typo in the element selector and if the function is called after the element is available.This function works for strings and arrays containing strings. If an argument or array value is not a string nor an array, it does not touch the value.
If your intention is to turn multiple whitespace to a single one (as in
string with multiple spaces in it->string with multiple spaces in it), you could also use the simplified RE:A single whitespace character will be replaced by a single space. Multiple whitespace characters will also be replaced by a single space. Note that I added the
gflag so the replacement occurs multiple times.