测试输入值是否与常量值匹配 [英] Test if input values match constant value

查看:31
本文介绍了测试输入值是否与常量值匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务,有点卡住了.

需要将输入字符串与常量中的值进行匹配,但我正在匹配单个字符.

我的常量是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆