views:

105

answers:

3

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
]
+2  A: 

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.

NullUserException
Neither of them work for me unfortunately, it won't even do what it did before. :/
Joshua
@Joshua See my updated answer
NullUserException
That still doesn't work for me, but now you pointed out that the string may have something do with it. This is the actually string, not sure whether or not it would affect it though: `(MyStringIsOneWholeWord *)`.
Joshua
`*` is also not included in the character class `\w`. Try modifying the regex to `/(?:^|\s|\()+(My\w+)\W/g`
NullUserException
Still no luck with that either.
Joshua
@Joshua Please post the rest of your code. I am sure these work.
NullUserException
Also, what exactly do you want to capture if the input is `(MyStringIsOneWholeWord *)`, `MyStringIsOneWholeWord` or `MyStringIsOneWholeWord *`?
NullUserException
Updated my original post and I am trying to capture `MyStringIsOneWholeWord`.
Joshua
@Joshua Answer edited. Bad news included.
NullUserException
Hmmm, I see. So it's a combination of SHJS and javascript's restrictions. Thanks for the information. :)
Joshua
+1  A: 

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+.*)\)/
M42
A: 

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

mkoistinen
Unfortunately I don't think the OP can use any of these workarounds. Interesting link though.
NullUserException