通过RegExp匹配大写字母和小写字母

发布于 2024-09-26 02:12:58 字数 253 浏览 2 评论 0原文

我写了一些像这样的正则表达式模式:

SomeText

匹配

Sometext
sOMeTeXt
SOMETEXT
SoMEteXt

但我希望模式与:类似的东西

!事实上我想用这个

\s?[^a-zA-Z0-9\_]SomeText[^a-zA-Z0-9\_]

我该怎么办?

I wrote some RegExp pattren like this :

SomeText

But I want the pattren to match with :

Sometext
sOMeTeXt
SOMETEXT
SoMEteXt

Somethings like that !

In fact I want to use this

\s?[^a-zA-Z0-9\_]SomeText[^a-zA-Z0-9\_]

what should i do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

久隐师 2024-10-03 02:12:58

在许多正则表达式实现中,您可以指定应用于模式给定部分的修饰符。不区分大小写是这些修饰符之一:

\s?[^a-zA-Z0-9\_](?i)sometext(?-i)[^a-zA-Z0-9\_]

(?i)(?-i) 之间的部分将被置于不区分大小写模式。根据此比较表,如果您使用 .net、Java、 Perl、PCRE、Ruby 或 JGsoft 引擎。

当然,由于您在字符类中指定了 azAZ,因此您可以简化并在整个模式上使用不区分大小写的修饰符:

/\s?[^a-z0-9\_]sometext[^a-z0-9\_]/i

In many regex implementations, you can specify modifiers that apply to a given part of your pattern. Case-insensitivity is one of those modifiers:

\s?[^a-zA-Z0-9\_](?i)sometext(?-i)[^a-zA-Z0-9\_]

The section between (?i) and (?-i) will be put into case-insensitive mode. According to this comparison table, this is supported if you're using .net, Java, Perl, PCRE, Ruby or the JGsoft engine.

Of course, since you're specifying both a-z and A-Z in your character classes, you could simplify and use the case-insensitive modifier on the entire pattern:

/\s?[^a-z0-9\_]sometext[^a-z0-9\_]/i
过潦 2024-10-03 02:12:58

如果您无法使用修饰符:

[Ss][Oo][Mm][Ee][Tt][Ee][Xx][Tt]

In case you cannot use modifiers:

[Ss][Oo][Mm][Ee][Tt][Ee][Xx][Tt]
独自唱情﹋歌 2024-10-03 02:12:58

您可以使用忽略大小写修饰符:

/sometext/i

You could use the ignore case modifier:

/sometext/i
叹梦 2024-10-03 02:12:58

Javascript:可以以任何顺序使用大写和小写字母。

value.match(new RegExp(searchkey, "i")) 

Javascript: Works both capital and small letter in any order.

value.match(new RegExp(searchkey, "i")) 
终弃我 2024-10-03 02:12:58

如果您需要使用变量名称创建 regEx 并在不区分大小写的情况下进行全局比较,请使用下面的示例

let str = 'Basappa';
var regex = new RegExp(str, "gi");

In case if you need to create regEx using the variable names and compare globally with case insensitive use the below example

let str = 'Basappa';
var regex = new RegExp(str, "gi");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文