2

I have a string like this string(1), I want to get substring remove the last part to obtain just string any suggestions please!!

PS.the string could be: sringggg(125).

0

3 Answers 3

3

You can use various options to get the desired string.

//With regular expression with split() and fetch the first element of array
console.log('sringggg(125)'.split(/\(\d+\)/)[0]);

//Using string with split() and fetch the first element of array
console.log('sringggg(125)'.split('(')[0]);

//Using substr and indexOf
var str = 'sringggg(125)'
console.log(str.substr(0, str.indexOf('(')));

References

Sign up to request clarification or add additional context in comments.

Comments

0

If string is always in same pattern and ' ( ' is a separator use split .

var string =  'string(1)'
var result = string .split('(')[0];

Comments

0

Try using regexp

var tesst = "string(1)"
var test = tesst.match(/^[^\(]+/);
// test = "string"

Comments