admin管理员组文章数量:1400089
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 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 :(
Share Improve this question edited Jun 14, 2011 at 13:09 Frederik Kammer asked Jun 14, 2011 at 12:17 Frederik KammerFrederik Kammer 3,1773 gold badges30 silver badges29 bronze badges 3-
What is
array1
exactly? An array of what? Strings? – Lekensteyn Commented Jun 14, 2011 at 13:28 - Yes I think so. But to be honest I'm not 100% sure because it's generated by an XML-Parser ... Is there any way to check it? Like var_dump in PHP? – Frederik Kammer Commented Jun 14, 2011 at 13:38
-
1
Some PHP functions have been implemented in Javascript,
var_dump
is one of them. – Lekensteyn Commented Jun 14, 2011 at 13:55
2 Answers
Reset to default 3I 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(/\s\s+/, "");
// 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.
I assume input
is your input textbox or something?
In which case, input doesn't have a replace()
function defined for it. You want to make sure you're calling replace()
on a string - the value inside the input, not the input itself. Try this:
function maskString(input) {
return input.value.replace(/\s\s+/," ");
}
本文标签: javascriptTypeError Result of expression 39inputreplace39 undefined is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: Result of expression 'input.replace' [undefined] is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744257532a2597553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论