tags:

views:

58

answers:

3

Hi There

I need some help with a regular expression, please help if you can

I have the following code: I am using Javascript and ASP

{In|inside|during|into|in the sphere of} {this} {article|piece of writing|editorial|commentary|paragraph|section} {we} {will|desire to|wishto|want to|resolve to|will} {tell} {you} {more} {about|regarding|with reference to} {the}

The desired code should look like this:

{In|inside|during|into|in the sphere of} this {article|piece of writing|editorial|commentary|paragraph|section} we {will|desire to|wishto|want to|resolve to|will} tell you more {about|regarding|with reference to} the

The brackets around the single words with no | should be removed like - this - we - tell you more - the in the example above.

I am thinking that the solution should be something like this

replace(/{.+?[^\|]/ig, '');   

to replace the { there should not be a | in the code; {.+?[^\|] and replace { with nothing

Then if there is not a starting { to replace the } with nothing

I am not sure how to do this, and how to only remove the {} where there is no | inside without removing the content...

+1  A: 

Try:

var string = "{hello|there} {yes} {no|me} {ok}";
string = string.replace(/{[A-Za-z0-9]+)}/g, "$1");

Gives you:

{hello|there} yes {no|me} ok
Vivin Paliath
what if there are numbers in the string?
Mark E
Thanks vivin - Your sulution works, except fot the fact that if there is a number in the string I will loose it... but thanks a million appreciate the help!
Gerald Ferreira
@Gerald ah, since you said "words", I thought just "letters". :) I've edited the solution.
Vivin Paliath
+2  A: 
x.replace(/{([^|}]*)}/g, '$1')
Dominic Cooney
+1, beat me to it by a few seconds
Mark E
Thanks Mark - works 100% I like your solution the best it leaves numbers in the text like {10|ten} 10 and ten...
Gerald Ferreira
A: 

Javascript isn't my thing, the equivalent of this works for me in another language.

replace(/\{([^|]+?)\}/g,"$1")

MattH
Looking at the other answers that were posted while I was working this out, I guess that the `{` and `}` don't need escaping.
MattH
Yeah I was wondering the same thing. I believe it depends on the implementation. It always bites me when I use regexes in vi or vim because you have to escape some characters.
Vivin Paliath