/    Sign up×
Community /Pin to ProfileBookmark

I’m trying to modify this Regex:

RegExp(‘{$[a-zA-Z]+ .+}’, ‘g’)

where it only returns a match for this text:
{$REGION ‘Designer Managed Code’}

Help???? I am horrible at regex…just can’t understand them.

to post a comment
JavaScript

26 Comments(s)

Copy linkTweet thisAlerts:
@FangFeb 01.2005 — What do you want it to match?
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — I want to match the entire thing between '{$Region' and the

ending '}'
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — The regex posted matches all of these:

{$REGION 'Designer Managed Code'}

{$IFDEF VER140}

{$ELSE}

{$ENDIF VER140}

{Syntax Highlighting}

{$R *.dfm}

I want one that matches only the {$Region '...'} directive.
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — Hi,

Well, I just learned something new: when you use the RegExp constructor, you have to escape() escape symbols. For instance, since braces { and dollar signs $ are special regex symbols, you have to escape them to literally look for those characters. However, with the string argument in the RegExp constructor that doesn't work, and you have to escape the escape symbols. In a regular regex, you would do this:

$

to search for a $ sign, but with the RegExp constructor, you have to do this:

$

I would recommend not using a RegExp constructor unless necessary, so you can avoid having to escape escape symbols. Instead just do this:

var my_regex = /regex expression/;

So, in your case, you can do this:

var r =/{$.*}/;

or equivalently:

var r = RegExp("{$.*}");

You didn't give much detail on what kind of match you wanted. Those regexs match any opening brace, followed by a dollar sign, followed by character 0 or more times, followed by a closing brace.

Here is a regex tutorial for beginners:

tutorial:

http://www.sitepoint.com/article/expressions-javascript

list of symbols:

http://www.siteexperts.com/tips/functions/ts23/page1.asp
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — var r =/{$REGION.*}/;

In case you didn't know, the / at the beginning and the / at the end are the regex 'delimiters', which are similar to quote marks for strings. If you take those away for a minute, so you can see the regex by itself, it may be clearer to you what it is doing:

{$REGION.*}

Here is a list of the symbols:
<i>
</i>{ opening brace
$ a dollar sign
REGION the letters REGION
. any character
* previous character is matched 0 or more times
} closing brace

As I mentioned above, normally when you are trying to match a certain character like an exclamation mark or letters, you would just use the character:

!

or

A

but braces and dollar signs are regex symbols that are used to mean other things, so they are like reserved words, and if you literally want to match those characters, you have to escape() them.
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — Neither of those work. I am teting in Expresso, is that a problem?
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — ooops. I forgot the 'g' which stands for global matching, i.e. it will match all occurrences--not just the first occurence:

var r =/{$REGION.*}/g;
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — 7stud, yours really did work. I copied it incorrectly.

However, how can I match $Region and $REGION and $region
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — I am teting in Expresso, is that a problem?[/QUOTE]
Huh?
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — [i]Originally posted by 7stud [/i]

[B]ooops. I forgot the 'g'...[/B][/QUOTE]


IT caused it to not match when I added it to the expression in Expresso.
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — Add an 'i' after the 'g' for a case insensitive search.
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — [i]Originally posted by 7stud [/i]

[B]Huh? [/B][/QUOTE]


Expresso is a Regular Expression parser to test regular expressions.

Freeware: http://www.ultrapico.com/
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — [i]Originally posted by 7stud [/i]

[B]Add an 'i' after the 'g' for a case insensitive search. [/B][/QUOTE]


Which 'g'?
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — IT caused it to not match when I added it to the expression in Expresso.[/QUOTE]
I don't know what Expresso is. If there will only be one match per string, don't worry about it. If there is more than one occurrence and you need to match them all, you'll have to go to an Expresso forum and ask about it.
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — ...or search google for something like "regular expressions Expresso global matching"
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — I'm only testing the expression in Expresso. I intend to modify a syntax highlighter to add colorizing for the '{$Region ..}' directive.

Expresso uses the .Net Regex so that may be why these don't match anything:

/{$REGION.*}/g

or

/{$REGION.*}/gi
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — Expresso is a Regular Expression parser to test regular expressions.[/QUOTE]
Then, why should you care what it says? It works in Javascript.
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — Maybe it requires the RegExp contructor format--try that.
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — var r = RegExp("{$REGION.*}", "gi");
Copy linkTweet thisAlerts:
@herodote92Feb 01.2005 — Yep, came up to something similar. If it's not exactly what you're looking for, there should be little to change.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script language=javascript&gt;
var r =/{$REGION.*}/gi;
var x = "{$REGIONfoo}";
// works also with {$regionfoo} {$RegionFoo} {$rEgIoNFOO} etc.
alert(r.test(x));
&lt;/script
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — [i]Originally posted by 7stud [/i]

[B]var r = RegExp("{$REGION.*}", "gi"); [/B][/QUOTE]


Yepper that worked. but I am having trouble with the origrinal one, RegExp('{$[a-zA-Z]+ .+}', 'g').

It is ALSO matching the {$REGION..} and I am getting its text doubly

replaced.

I can't explain it more without going into how the syntax highlighter works. It is located here:

[URL]http://www.dreamprojections.com/SyntaxHighlighter/Default.aspx[/URL]
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — So essentially, I have the one that matches the $Region directive, now the other one need to EXCLUDE the $Region directive.
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — Depending on what you are doing, you can try reversing the order in which you run the two regexes and see if that works.
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — Same thing happens, is ther any way to exclude the matches

for {$Region ..} from the other one?
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — Can backreferencing be used for this?
Copy linkTweet thisAlerts:
@MrBaseball34authorFeb 01.2005 — After further review, this is not even needed as the {$REGION ..} directive is supposed to be colored the same as the other compiler

directives.

Thanks for all your help.
×

Success!

Help @MrBaseball34 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...