正则表达式:读取数字后面的字符

发布于 2024-09-26 14:04:09 字数 766 浏览 2 评论 0原文

我有正则表达式代码:

<script type="text/javascript">
     var str = "kw-xr55und";
     var patt1 = /[T|EE|EJU].*D/i;
     document.write(str.match(patt1));
</script>

它可以读取:

str= "KD-R55UND" -> as UND

但如果我输入:

 str= "kw-tc800h2und ->  result tc-800h2und.  //it makes script read T in front of 800
 i want the result as UND 

如何使代码仅检查 800 后面的字符?

编辑

此代码后它可以工作:

<script type="text/javascript">
var str = "kw-tc800h2und";
var patt1 = /[EJTUG|]\D*D/i;
document.write(str.match(patt1));
</script>

但显示下一个问题,我可以显示结果,如果:

str= "kw-tc800un2d"

i want result like -> UN2D

i have regex code:

<script type="text/javascript">
     var str = "kw-xr55und";
     var patt1 = /[T|EE|EJU].*D/i;
     document.write(str.match(patt1));
</script>

it can read:

str= "KD-R55UND" -> as UND

but if i type:

 str= "kw-tc800h2und ->  result tc-800h2und.  //it makes script read T in front of 800
 i want the result as UND 

how to make the code just check at character behind the 800?

EDIT

After this code it can work:

<script type="text/javascript">
var str = "kw-tc800h2und";
var patt1 = /[EJTUG|]\D*D/i;
document.write(str.match(patt1));
</script>

but show next problem, i can show the result if:

str= "kw-tc800un2d"

i want result like -> UN2D

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

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

发布评论

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

评论(4

往日 2024-10-03 14:04:09

试试这个:

 var patt1 = /(T|EE|EJU)\D*$/i;

它将匹配以 TEEEJU 开头并在字符串末尾结束的非数字字符序列。如果字符串必须像示例中那样以 D 结尾,您可以将其添加到:

 var patt1 = /(T|EE|EJU)\D*D$/i;

如果您想在任何地方匹配它,而不仅仅是在字符串末尾,请尝试以下操作:

 var patt1 = /(T|EE|EJU)\D*D/i;

编辑:哎呀!不,这当然行不通。我试图猜测 [T|EE|EJU] 的意思,因为它是一个与一个字符 E 匹配的字符类, JTU|(相当于 [EJTU|]) ,我确信这不是你的意思。但到底是什么,试试这个:

 var patt1 = /[EJTU|]\D*D/i;

我仍然不明白你想做什么,但有时尝试和错误是前进的唯一方法。至少这次我测试了! :P

编辑:好的,所以匹配可以包含数字,只是不能以一开头。试试这个:

var patt1 = /[EJTU|]\w*D/i;

Try this:

 var patt1 = /(T|EE|EJU)\D*$/i;

It will match a sequence of non-digit characters starting with T, EE or EJU, and finishing at the end of the string. If the string has to end with D as in your examples, you can add that in:

 var patt1 = /(T|EE|EJU)\D*D$/i;

If you want to match it anywhere, not just at the end of the string, try this:

 var patt1 = /(T|EE|EJU)\D*D/i;

EDIT: Oops! No, of course that doesn't work. I tried to guess what you meant by [T|EE|EJU], because it's a character class that matches one of the characters E, J, T, U or | (equivalent to [EJTU|]), and I was sure that couldn't be what you meant. But what the heck, try this:

 var patt1 = /[EJTU|]\D*D/i;

I still don't understand what you're trying to do, but sometimes trial and error is the only way to move ahead. At least I tested it this time! :P

EDIT: Okay, so the match can contain digits, it just can't start with one. Try this:

var patt1 = /[EJTU|]\w*D/i;
桃扇骨 2024-10-03 14:04:09

对于 PCRE,请尝试以下操作:

/(?<=\d)\D*/

它使用后向查找来查找紧随数字之后的一组非数字字符。

对于 Javascript,请尝试以下操作:

/\D+$/

它将匹配从文本末尾向后算起的所有非数字字符。

For PCRE, try this:

/(?<=\d)\D*/

It uses a lookbehind to find a set of non-digit characters that comes immediately after a digit.

For Javascript, try this:

/\D+$/

It will match any characters that are not digits from the end of the text backwards.

找回味觉 2024-10-03 14:04:09

根据您使用的正则表达式的风格,您可以使用断言背后的外观。它本质上会说“如果它就在数字后面,则匹配它”。

在 python 中,它是这样的:

(?<=\d)\D*

哦,还有,正则表达式区分大小写,除非你设置不区分大小写。

Depending on what flavor of regex you're using, you can use a look behind assertion. It will essentially say 'match this if it's right after a number'.

In python it's like this:

(?<=\d)\D*

Oh, also, regex is case sensitive unless you set it not to be.

酷炫老祖宗 2024-10-03 14:04:09

如果其中可以有 1 位数字,但不能有 2 位数字,请尝试 /[TEUJ]\D*\d*\D*D$/i。获取更具体的信息将需要额外的信息,例如字符串的最大长度,或者解析 tc800h2undh2und 之间的具体区别。

Try /[TEUJ]\D*\d*\D*D$/i if it can have 1 digit in it, but not 2. Getting any more specific would require additional information, such as the maximum length of the string, or what exactly differs between parsing tc800h2und and h2und.

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