Total noob here, I'm currently studying regex in javascript. I have a function that is supposed to pick numbers between 0 and 9 from a string. The function works fine as long as the variable it searches has letters or numbers, however when a null value is entered it gives out the following error:
Uncaught: TypeError: Cannot read property 'toString' of null
How can I fix this? Thank you in advance.
Here is the code:
var lause = "s0m3 p30pl3";
function tulostanumerot();
var numerot = /[0-9]/g;
var testi = numerot.test(0-9);
var setti = lause.match(numerot);
if (testi === true) {
console.log(setti.toString());
}
else {
console.log("Ei numeroita!");
};
Total noob here, I'm currently studying regex in javascript. I have a function that is supposed to pick numbers between 0 and 9 from a string. The function works fine as long as the variable it searches has letters or numbers, however when a null value is entered it gives out the following error:
Uncaught: TypeError: Cannot read property 'toString' of null
How can I fix this? Thank you in advance.
Here is the code:
var lause = "s0m3 p30pl3";
function tulostanumerot();
var numerot = /[0-9]/g;
var testi = numerot.test(0-9);
var setti = lause.match(numerot);
if (testi === true) {
console.log(setti.toString());
}
else {
console.log("Ei numeroita!");
};
Share
Improve this question
asked Aug 1, 2021 at 11:44
jope1994jope1994
211 silver badge2 bronze badges
3
-
2
This part does not look correct
var testi = numerot.test(0-9);
– The fourth bird Commented Aug 1, 2021 at 11:49 - 2 The function declaration is incorrect; there's no function body. – Pointy Commented Aug 1, 2021 at 11:51
- can you provide a minimal reproducible example showing the error, the code you provided doesn't produce the type error you described in your question – Nick Parsons Commented Aug 1, 2021 at 12:01
4 Answers
Reset to default 2The String.prototype.match() returns null
if no matches are found.
So, you have two options to handle this
- check if
setti
have a truthy value, you can that like thisif(setti)
. - Use Optional chaining
(?.)
likesetti?.toString()
, .
A few notes about your code:
The method test() takes a string argument, which makes this not correct
var testi = numerot.test(0-9);
The code is not inside the function
function tulostanumerot();
You can omit
testi
at all, and only usesetti
Note that match() returns either an array or null, so you can use
if (setti) {
instead of checking for true
The code might look like
function tulostanumerot() {
var numerot = /[0-9]/g;
var setti = lause.match(numerot);
if (setti) {
console.log(setti.toString());
} else {
console.log("Ei numeroita!");
}
}
tulostanumerot();
function tulostanumerot(lause) {
var numerot = /[0-9]/g;
var setti = lause.match(numerot);
if (setti) {
console.log(setti.toString());
} else {
console.log("Ei numeroita!");
}
}
[
"s0m3 p30pl3",
"",
"test"
].forEach(v => tulostanumerot(v));
If you do some console.log
ging you will see that lause.match()
returns an array of numbers where the match was found.
In your case:
["0", "3", "3", "0", "3"]
You are getting an error because setti
will be null if no matches are found. We can check for it like so.
if (setti) {
// Setti is not undefined
}
Then if you want to bine the elements into a string you can use .join
instead.
if (setti) {
console.log(setti.join());
} else {
console.log("Ei numeroita!");
};
Full code:
var lause = "s0m3 p30pl3";
function tulostanumerot() {
var numerot = /[0-9]/g;
var setti = lause.match(numerot);
if (setti) {
console.log(setti.join(""));
} else {
console.log("Ei numeroita!");
};
}
var lause = "s0m3 p30pl3";
tulostanumerot()
var lause = "no numbers here";
tulostanumerot()
There is several ways to check for this. And sould not be hard to find anything online :)
if(lause) //Will check if there is any value to the string, which is a non false value. Example: 0, null, undefined false will all fail this statement
OR
if(typeof lause !== "undefined") //Simple but will also pass Arrays and objects
if(typeof lause === "string" && typeof lause === "number") //Will only be passed if matching. So if the variable is a array or object it will not be passed
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745247542a4618480.html
评论列表(0条)