1

My option looks like this:

<option value="80">a1, a2, a3, a4, a5 (80)</option>

With this code:

str_split = str.split(',');

i receive this:

a1 a2 a3 a4 a5 (80)

But i want to remove the id with the braces to get this:

a1 a2 a3 a4 a5

The id can vary like "(8)" or "(20)" or "(100)" and so on..


I tried it with

str_replace = str.replace(/[()]/g, "");

but it gives me this result:

a1 a2 a3 a4 a5 80

How do I do that?

EDIT :

is it possible to do this with the last braces with id from string?

just tested it and got problems when my option looks like this

<option value="80">a1(12), a2(test), a3(333), a4(xx), a5(34) (80)</option>

i want this

a1(12) a2(test) a3(333) a4(xx) a5(34)
bernte
  • 1,184
  • 2
  • 19
  • 34
  • You can use `strstr` like in PHP but you will need this function. http://stackoverflow.com/questions/7015181/jquery-or-javascript-strstr – Alireza Jul 27 '13 at 19:30

2 Answers2

6

You can use

var str_replace = str.replace(/\([^)]+\)$/,'')

How it works :

  • \( : escaped opening parenthesis
  • [^)] : not a closing parenthesis
  • + : at least once
  • \) : closing parenthesis, escaped
  • $ : end of string
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    If you want to remove it only at end of the string, use `str.replace(/\([^)]+\)$/,'')`. Is that your new question ? If you want to match only digits, use `str.replace(/\(\d+\)$/,'')`. – Denys Séguret Jul 27 '13 at 19:50
1

You can use both .split() and .replace() to do this:

  1. Using .split():

    var str_split = str.split(' (');
    var str_replace = str_split[0];
    
  2. Using .replace():

    var str_replace = str.replace(/ \([0-9]+\)/, "");
    

jsFiddle here

Petr R.
  • 1,247
  • 2
  • 22
  • 30