jquery检查字符串是否以1234开头 [英] jquery check if string starts with 1234

查看:57
本文介绍了jquery检查字符串是否以1234开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出宝贵时间回答我的问题。

Thanks for taking the time to answer my question.

我想检查字符串是否有7个字符并以1234开头。我该怎么做?

I want to check if a string has exactly 7 characters and starts with "1234". How do I do that?

我知道string.substring但不确定我是否应该使用正则表达式还是有其他选择。提前致谢!

I know of string.substring but am not sure if I shouold use regex or are there any other alternative. Thanks in advance!

推荐答案

简单的RegExp:

var isMatch = /^1234...$/.test(myString);

或者:

var isMatch = myString.length == 7 && myString.indexOf("1234") == 0;

编辑或:

var isMatch = myString.length == 7 && myString.substr(0, 4) == "1234";

这篇关于jquery检查字符串是否以1234开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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