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)
.
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)
.
3 Answers
Reset to default 3You 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
- String.prototype.split()
- String.prototype.indexOf()
If string is always in same pattern and ' ( ' is a separator use split .
var string = 'string(1)'
var result = string .split('(')[0];
Try using regexp
var tesst = "string(1)"
var test = tesst.match(/^[^\(]+/);
// test = "string"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745112465a4611926.html
评论列表(0条)