测试输入值是否与常量值匹配 [英] Test if input values match constant value
问题描述
我有一个任务,有点卡住了.
需要将输入字符串与常量中的值进行匹配,但我正在匹配单个字符.
我的常量是 ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUWXYZ'
我的输入是,var input = 'ABOZ'
我需要一个测试来检查输入变量中的每个字母是否存在于 ALPHABET 常量中.
希望我说得有道理.
干杯
这是您问题的单行答案:
(ALPHABET.match(new RegExp((input.split('').join('|')), 'g'))).length == input.length
>
仅当 input
中的所有字符都出现在 ALPHABET
true
这是一个工作演示 http://jsfiddle.net/kayen/akL4A/
Ive got an assignment and am a bit stuck.
Need to match an input string to the values in a constant, but I am matching individual characters.
My constant would be ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUWXYZ'
My input would be, var input = 'ABOZ'
I need a test to check if each letter in the input variable exist in the ALPHABET constant.
Hope I made sense.
Cheers
Here's a single line answer to your question:
(ALPHABET.match(new RegExp((input.split('').join('|')), 'g'))).length == input.length
which would return true
only if all the characters in input
are present in ALPHABET
Here's a working demo http://jsfiddle.net/kayen/akL4A/
这篇关于测试输入值是否与常量值匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!