Reputation: 297
I have a string with sub-string '5+' and it looks like sales/100/5+
. I need to remove the sub-string 5+
.
Input : sales/100/5+
Demanded output : sales/100
The jquery script is as follows:
var rstring = 'sales/100/5+';
if((splitStr == '5+')||(splitStr == '4-')) {
var key = rstring.replace(new RegExp('/'+splitStr, 'g'),"");
alert(key);
}
Now im getting the result : sales/100+
But desired output is : sales/100
Upvotes: 0
Views: 97
Reputation: 11388
You don't really need to use a Regex to make your string replace work
the simplest the best :
var rstring = 'sales/100/5+';
if((splitStr == '5+')||(splitStr == '4-'))
{
var key = rstring.replace('/' + splitStr, '');
alert(key);
}
the '+' in your Regex is a quantifier. It matches between one and unlimited times, as many times as possible, giving back as needed. That's why your + was still ther after your replace.
Upvotes: 1
Reputation: 3207
You can remove /5+
from given strings. No jQuery code is neede, just a simple JS function.
function replace5plus(str) {
return str.replace('/5+', '');
}
Upvotes: 0
Reputation: 337560
To achieve this you can split()
the string by /
, remove the last element, then join()
it back together, like this:
var arr = 'sales/100/5+'.split('/');
arr.pop();
console.log(arr.join('/'));
Upvotes: 1