1

I am admittedly NOT a regex person but usually I can figure my way around something. This one has me stumped...

I need to match and replace a double greater than str (>>) that has an optional leading space. I know this doesn't work but something along the lines of...

/\s\s+[>>]/

But that's obviously no good.

Would appreciate any help. This site has been an amazing resource for me over the years and I can't believe I'm only getting around to posting something now, so it goes to show even a knucklehead like me has been able to benefit without bothering people... until now:) Thanks in advance.

1
  • 1
    do you want it to replace the optional leading space in addition to the >> if found? Commented Mar 14, 2013 at 21:37

7 Answers 7

1

For >> both inside a string and with leading whitespace, try:

/(\s*)(>>){1}/
Sign up to request clarification or add additional context in comments.

1 Comment

you could just use />>/ but i guess the regex above makes the replacement job easier.
0

If you want the space to be optional, then you can simply do this :

/>>/

And you may use it as a replacement pattern with the g modifier :

str = str.replace(/>>/g, 'something') 

If you want to check that a string is >> with maybe some space before, then use

/^\s?>>$/

4 Comments

or just '>>' for simple string replaces
@WouterJ If you want to do more than one replacement, you must use a regex : myString.replace(/>>/g, '')
oh? I thought the g modifier was only usefull in matching, not in replacing. (in PHP, all replaces will be global by default)
Thanks dystroy -- /\s?>>$/ ended up being the winner (without the caret because the pattern might not be expressly at the head of the string). Testing now and seems to be working flawlessly. Thank you!
0

Breaking down your example:

  1. \s will match any white-space character
  2. \s+ will match one or more white-space characters
  3. [>>] will match one > (see more below on this)

So your expression will match a > preceeded by at least two white-space characters.

If you want to match zero-or-more you will have to use *; fex \s*.

Square brackets are used to denote sets of characters, and will match any of the characters in the set; fex [abc] will match a, b or c, but only one character at time.

Single characters in a regular expression will match that character; fex > will match one greater-than sign.

Putting it together we get the following regular expression for your case:

/\s*>>/

Comments

0

If you want it to match an unlimited amount of leading space characters:

/ *>>/

If you want it to match 0 or 1 leading space character:

/ ?>>/

1 Comment

+ means one or more, use ? for zero or one.
0

use this :

str.replace(/\s+>>/g, 'whatever'); 

Comments

0

This regex should work.

/\s*[>]{2}/

This is cleaner

/\s*>>/

Tested:

var pattern = /\s*>>/;
s1 = "             >>";
s2 = ">>";
s3 = ">> surrounding text";
s4 = "surrounding  >> text";
s5 = "surrounding>>text";

s1.match(pattern);
["             >>"]
s2.match(pattern);
[">>"]
s3.match(pattern);
[">>"]
s4.match(pattern);
["  >>"]
s5.match(pattern);
[">>"]

Replacement example

var pattern = /\s*>>/;
var s6 = "    >> surrounding text";
s6.replace(pattern, ">");
"> surrounding text"

1 Comment

Or just >> for that matter.
0

This should work with the optional space.

/\s{0,}>>/g

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.