用于检查字符串是否以字符串开头的正则表达式,忽略大小写差异 [英] Regex to check whether string starts with, ignoring case differences

查看:324
本文介绍了用于检查字符串是否以字符串开头的正则表达式,忽略大小写差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查单词是否以特定子字符串开头,忽略大小写差异。我一直在使用以下正则表达式搜索模式进行此检查,但是当字符串中存在差异时,这无效。

I need to check whether a word starts with a particular substring ignoring the case differences. I have been doing this check using the following regex search pattern but that does not help when there is difference in case across the strings.

我的区分大小写方式:

var searchPattern = new RegExp('^' + query);
if (searchPattern.test(stringToCheck)) {}


推荐答案

i 修饰符作为第二个参数传递:

Pass the i modifier as second argument:

new RegExp('^' + query, 'i');

看看文档

这篇关于用于检查字符串是否以字符串开头的正则表达式,忽略大小写差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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