开发者

RegEx: Find word after a letter but don't include the letter in the result

Here's a string that I may have:

开发者_如何学Go(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
]


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.


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


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+.*)\)/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜