1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Javascript to check string value and remove if it matches a pattern?

Discussion in 'General PHP and MySQL Discussions' started by Kevin, Jun 15, 2014.

  1. Kevin

    Kevin Well-Known Member

    Is this doable in javascript?

    If I have a string like "James and the Giant Peach (tt0116683)" (without the quotes) I want to see if the string ends with an opening & closing parenthesis and if what is in the parenthesis starts with "tt" then, if it meets that criteria, to remove the content in the parenthesis & the parenthesis characters.

    In the example of "James and the Giant Peach (tt0116683)" it'd be see if the string ends with "(tt{value})" and if the condition is true then end up with just "James and the Giant Peach".
  2. AndyB

    AndyB Well-Known Member

    Is this a thread title?
  3. EQnoble

    EQnoble Well-Known Member

    yes within JS you can get the contents of a set of tags and run a regex match against them and from there you can do whatever you want including updating html to reflect the changes you specified.
  4. Kevin

    Kevin Well-Known Member

    Happen to know what the regex match would be?
  5. Kevin

    Kevin Well-Known Member

    It will end up being inserted into the text editor. That part I have working, just need to clean up the formatting of it now.
  6. Luke F

    Luke F Well-Known Member

    "James and the Giant Peach (tt0116683)".replace(/( ?\(tt\d{7}\))$/, "")
    Kevin likes this.
  7. Kevin

    Kevin Well-Known Member

    Luke, excellent, thank you! One quick question... is it possible to make the length any value that meets the match instead of explicit 7?
  8. Luke F

    Luke F Well-Known Member

    "James and the Giant Peach (tt0116683)".replace(/( ?\(tt\d+\))$/, "")
  9. Kevin

    Kevin Well-Known Member

    Thanks, that's giving the results exactly that I was looking for. (y)

Share This Page