You are viewing limited content. For full access, please sign in.

Question

Question

JavaScript remove round brackets

asked on December 12, 2017 Show version history

I am trying to use Regex to remove round brackets and whatever is inbetween them.

Sample string - Chess Club (ID:CHESS)

Sample string - Table Tennis (ID:TAB T)

//Regex SUKEY
$(document).ready(function () {
 
  var re = new RegExp("(.*\\b)\(ID:.*\)");
 
  $('#q137 input').on('change', function() {
    var pm = $(this).val().match(re);
    $('#q138 input').val(pm[1]).trigger('change');
  });
})

 

Returns the value - Chess Club (

0 0

Answer

SELECTED ANSWER
replied on December 21, 2017

Is something like this what you're looking for?

$(document).ready(function() {
  $('#q137 input').on('change', function() {
    var replacement = $(this).val().replace(/\(.*\)/, '');
    
    $(this).val(replacement);
  });
})

 

2 0

Replies

replied on December 13, 2017

Are the parentheses always at the end of the string? If so, you could just do it with a split.

For example, 

var pm = $(this).val().split('(')[0].trim();

This breaks the string at the first left parentheses, takes the first part and trims off the trailing white space.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.