Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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," ")
like image 563
John Magnolia Avatar asked Dec 07 '25 08:12

John Magnolia


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.

like image 177
aioobe Avatar answered Dec 08 '25 21:12

aioobe


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. http://myregextester.com/index.php

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
----------------------------------------------------------------------
like image 27
Chris Gessler Avatar answered Dec 08 '25 21:12

Chris Gessler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!