Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep numbers only from user input in javascript [closed]

Tags:

javascript

I found this and I don't know whether it is a function or what and I can't understand what it does

(/^\d+$/)

like image 448
Marco Maher Avatar asked Aug 14 '26 06:08

Marco Maher


2 Answers

It's a Regular Expression AKA RegExp MDN:

(/^\d+$/) 

() Are simple brackets (from a function call using RegExp as argument)

/ ... / is a Regex syntax matching:
^ from the start of a string
\d match numbers (akin to [0-9])
+ one or more times
$ till the end of string

like image 126
Roko C. Buljan Avatar answered Aug 16 '26 21:08

Roko C. Buljan


/ : js regex delimiter

^ : start of the string

\d: matches any single digit between 0 - 9

+ : multiple time and at least once

$ : end of the string

/ : Js regex delimiter

This means in summary match a string that contains only digit, once or multiple time. Example : "7" is valid, "77" is valid, "" is not valid

like image 43
RLoris Avatar answered Aug 16 '26 21:08

RLoris