正则表达式:查找字母后的单词,但不将该字母包含在结果中

发布于 2024-09-18 10:38:18 字数 439 浏览 3 评论 0原文

这是我可能拥有的字符串:

(MyStringIsOneWholeWord *)

我使用了以下 javascript 正则表达式来获取括号后面的文本(如果它以 My 开头)。

/(^|\s|\()+My(\w+)/g,

问题在于它在结果中包含第一个括号,因为它是找到它的字母/字符。

我如何去掉结果中的括号?


编辑

有关更多信息,我正在编辑SHJS语法荧光笔的C语言 javascript文件。

这是该问题的所有相关代码:

[
 /(^|\s|\()+My(\w+)/g,
 'sh_keyword',
 -1
]

Here's a string that I may have:

(MyStringIsOneWholeWord *)

I have used the following javascript regular expression to get the text after the bracket if it starts with My.

/(^|\s|\()+My(\w+)/g,

The problem with this is that it includes the first bracket in the result, as that it is the letter/character that found it.

How would I get rid of the bracket in the result?


EDIT

For more information, I am editing the C Language javascript file of the SHJS syntax highlighter.

Here's all the relevant code for this question:

[
 /(^|\s|\()+My(\w+)/g,
 'sh_keyword',
 -1
]

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

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

发布评论

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

评论(3

疾风者 2024-09-25 10:38:18

如果这只是 JS,您可以使用捕获组:

/(^|\s|\()+(My\w+)/g

然后在该组中获取匹配项。然而,SHJS 似乎将使用整个匹配,需要使用lookbehind,而Javascript 的正则表达式引擎不支持这一点。


为了解决这个问题,我建议您阅读文档。这部分在这里:

定义语言后,
您必须将其转换为 JavaScript
SHJS 使用的格式。您将需要
来自来源的 sh2js.pl 脚本
SHJS 的分布。 sh2js.pl
脚本是用 Perl 编写的并且需要
Parse::RecDescent 模块。

告诉我生成的 JS 文件不应该被编辑。文档说 SHJS 使用与 GNU Source-highlighting 相同的格式,指定 此处。因此,您应该编辑原始 .lang (link) 文件,然后将其转换为 .js

If this was just JS you could use a capture group:

/(^|\s|\()+(My\w+)/g

Then get the match at that group. However, it appears that SHJS will use the entire match, requiring the use of lookbehind, which is not supported by Javascript's Regex engine.


To get around this, I'd suggest you to read the documentation. This part here:

Once you have defined the language,
you must convert it to the JavaScript
format used by SHJS. You will require
the sh2js.pl script from a source
distribution of SHJS. The sh2js.pl
script is written in Perl and requires
the Parse::RecDescent module.

Tells me that the resulting JS files aren't meant to be edited. The docs say SHJS uses the same format as GNU Source-highlighting, which is specified here. So you should be editing the original .lang (link) files and then converting them to .js.

失而复得 2024-09-25 10:38:18

您想要的是一个积极的回顾断言。不幸的是,Javascript 不支持它们。然而,Steven Levithan 在博客文章中对此进行了介绍:http://blog.stevenlevithan。 com/archives/mimic-lookbehind-javascript

编辑:同一作者更新的文章:http://blog.stevenlevithan.com/archives/javascript-regex-lookbehind

What you want is a positive lookbehind assertion. Unfortunately, Javascript doesn't support them. However, Steven Levithan covers this in a blog post here: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

Edit: Updated article by same author: http://blog.stevenlevithan.com/archives/javascript-regex-lookbehind

感情洁癖 2024-09-25 10:38:18

我不明白你到底想捕获什么,但你可以尝试其中一些:

capture StringIsOneWholeWord in $1 :

/(?:^|\s|\()+My(\w+).*\)/

capture MyStringIsOneWholeWord in $1:

/(?:^|\s|\()+(My\w+).*\)/

capture MyStringIsOneWholeWord * $1 中的

/(?:^|\s|\()+(My\w+.*)\)/

捕获 $1 中的 StringIsOneWholeWord *

/(?:^|\s|\()+My(\w+.*)\)/

I don't understand what exactly you want to capture, but you can try some of these :

capture StringIsOneWholeWord in $1 :

/(?:^|\s|\()+My(\w+).*\)/

capture MyStringIsOneWholeWord in $1:

/(?:^|\s|\()+(My\w+).*\)/

capture MyStringIsOneWholeWord * in $1:

/(?:^|\s|\()+(My\w+.*)\)/

capture StringIsOneWholeWord * in $1:

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