Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  30] [ 5]  / answers: 1 / hits: 15180  / 13 Years ago, tue, june 14, 2011, 12:00:00

I wrote the following function in javascript:



function maskString(input) {
return input.replace(/ss+/, );
}


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 :(


More From » javascript

 Answers
0

I guess you have code like this:



var textarea1 = $(#textarea1);


textarea1.val() returns undefined if the element is not found (i.e. an element with ID textarea1). 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.



function maskData(input) {
// edit strings
if (typeof input == string) return input.replace(/ss+/, );

// if 'input' is an array, iterate through its elements and pass it to maskData again
if (input instanceof Array) {
for (var i=0; i<input.length; i++) {
input[i] = maskData(input[i]);
}
return input;
}
// otherwise just return itself untouched
return input;
// alternative: return an empty string
//return ;
}


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:



/s+/g


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 g flag so the replacement occurs multiple times.


[#91723] Monday, June 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidanl

Total Points: 156
Total Questions: 102
Total Answers: 112

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
aidanl questions
Thu, Feb 10, 22, 00:00, 2 Years ago
Thu, Dec 17, 20, 00:00, 4 Years ago
Fri, Jul 24, 20, 00:00, 4 Years ago
Thu, Jul 16, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
;