Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check if string contains a sequence of 18 numbers

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading