What is replace(/(<([^>]+)>)/ig," ") doing?

I have come across come code written by another developer and I can not working out what it is doing:

title.replace(/(<([^>]+)>)/ig," ")
2

2 Answers

It replaces all tags (substrings on the form <...>) with a space, " ".

Here's a regexp breakdown:

  • <       - a left tag
  • [^>] - anything but a right tag...
  • +       - ...one or more times
  • >       - a right tag.

The ( and ) just surrounds the groups in the expression, which are not used anyway.

The /ig suffix says that the regex is case insensitive (pointless in this case, since the rexeg doesn't mention any letters) and global stating that all occurrences should be replaced.

2

Looks like it's replacing HTML start or end tags. If you ever need to parse Regex expressions or test them, here's a great site.

NODE EXPLANATION
----------------------------------------------------------------------
(?i-msx: group, but do not capture (case-insensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally):
---------------------------------------------------------------------- ( group and capture to \1:
---------------------------------------------------------------------- < '<'
---------------------------------------------------------------------- ( group and capture to \2:
---------------------------------------------------------------------- [^>]+ any character except: '>' (1 or more times (matching the most amount possible))
---------------------------------------------------------------------- ) end of \2
---------------------------------------------------------------------- > '>'
---------------------------------------------------------------------- ) end of \1
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like