1

I am new with regex, could you help me to solve this?

I want to replace

Some text and XXblaaaaa# and text again XXbl!abla# or XXblalala# or XXbabla#

with

Some text and OOTEXT** and text again OOTEXT** or OOTEXT**

I made a jsfiddle to test t.

My regex for test is: /((XX).+?(?=(#)))/g;, i.e., find XX untill #.

But how can I set the following condition: untill # if string between XX and # does not contain !?

3
  • Why does XXbl!abla# or disappear from your string though? Commented Aug 22, 2013 at 12:42
  • Ah, now I understand. For each ! you want to ignore the next #, instead of skipping the entire XX..!..# thing. Commented Aug 22, 2013 at 13:02
  • Sorry, a open new question stackoverflow.com/questions/18382328/… Commented Aug 22, 2013 at 13:48

3 Answers 3

1
/XX[^!]*?#/g

This will replace everything from XX to #, as long as it doesn't include an !

Used like this:

"Some text and XXblaaaaa# and text again XXbl!abla# or XXblalala# or XXbabla#".replace(/XX[^!]*?#/g, "OOTEXT");

EDIT:

A more efficient version would be like this:

    /XX[^!#]*#/g

And if you don't want to allow whitespace, then add \s in the character class

/XX[^!#\s]*#/g
Sign up to request clarification or add additional context in comments.

11 Comments

Why not put the # inside the character class, too, to get rid of the ungreediness?
I think this way is easier to understand because it looks more like the OP's description. "From XX to # without including !"
Sure, but that doesn't mean we shouldn't advocate best practices ;).
@m.buettner: Sometimes readability over a [nominal] optimization is better. ;-)
I agree. But readability is also a best practice. I'll add it for completeness.
|
0

You can use the overload of .replace(regex,function):

var txt = 'Some text and XXblaaaaa# and text again XXbl!abla# or XXblalala# or XXbabla#';
var result = .replace(/(XX.*?#)/g,function(m,a){
  // only replace values that do not contain a '!'
  return a.indexOf('!') == -1 ? 'OOTEXT**' : a;
});
// "Some text and OOTEXT** and text again XXbl!abla# or OOTEXT** or OOTEXT**"

I'm not sure if your example was really that simple, so the above is a method with a lot of flexibility. You could also use [^!]*? in place of the .*?, but anything more complex then a single character will need to be more elaborate.

3 Comments

It does have lookaheads.
@m.buettner: indeed, I think was was thinking of look-behinds. There's one is has and one it doesn't, but never quite remember. ;-)
Yes, excactly. It doesn't have lookbehinds, but it does have lookaheads.
0

I think what you want is something like this:

result = input.replace(/XX[^!#]*(?:![^#]*#[^!#]*)*#/g, "OOTEXT**");

How does it work? We match XX, then we consume as many non-!, non-# characters as possible. The next group (?:![^#]#[^!#]*) matches a !, then consumes everything until the next # and continues with non-#, non-! as before. This part is repeated as many times as necessary, until we find a # that was not preceded by !.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.