I'm trying to do a check command with discord.js I need to know if a string has an id (a sequence of 17~19 numbers(ids have 17, 18 or 19 numbers)) example:
const string1 = "hello, my id is 286615441416257536, bla bla bla";
const string2 = "hello, bla bla bla";
//I want to pass a function that checks whether or not string 1 and 2 have an id
//string 1 should get true, string 2 should get false
//like:
console.log(hasId(string1)) //true
console.log(hasId(strin2)) //false
I know i can do it with .includes(...)
, but I don't understand about regex
CodePudding user response:
A simple regex: /\d{17,19}/
. \d
means "a number character", {17,19}
means "look for 17-19 (inclusive) of the previous thing" (which in this case is a number character)
const string1 = "hello, my id is 286615441416257536, bla bla bla";
const string2 = "hello, bla bla bla";
//I want to pass a function that checks whether or not string 1 and 2 have an id
//string 1 should get true, string 2 should get false
const hasId = str => /\d{17,19}/.test(str);
console.log(hasId(string1)) //true
console.log(hasId(string2)) //false