I am trying to implement a function in Javascript that verify a string. The pattern i must to do is contain only digits charactor, *, # and . For example:
182031203
12312312312*#
2131*1231#
*12312 #
#123*########
I tried alot of thing like
/^[\d]{1,}[*,#, ]{1,}$
but it's doesn't work. I am not sure that i understand good in regex. Please help.
CodePudding user response:
I think you want the pattern ^[0-9*# ] $
:
var inputs = [" 182031203", " 12312312312*#", " 2131*1231#", "12312 #", "#123########"];
for (var i=0; i < inputs.length; i) {
console.log(inputs[i] " => " /^[0-9*# ] $/.test(inputs[i]));
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Using Regex101
/^[0-9\*\#\ ] $/g
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive) * matches the character * with index 4210 (2A16 or 528) literally (case sensitive) # matches the character # with index 3510 (2316 or 438) literally (case sensitive) matches the character with index 4310 (2B16 or 538) literally (case sensitive)
CodePudding user response:
Try this regular expression:
const rxDigitsAndSomeOtherCharacters = /^(\d |[*#] ) $/;
Breaking it down:
^
start-of-text, followed by(
a [capturing] group, consisting of\d
one or more digits|
or...[*# ]
one ore more of*
,#
or
)
, the [capturing] group being repeated 1 or more times, and followed by$
end-of-text