I am trying to replace everything with blank after question mark.
Suppose I have a string like below:
var str = "/root/Users?SkillId=201;"
Now I want to replace everything with blank after ?
.
Expected output: "/root/Users"
I tried below solution:
var str = "/root/Users?SkillId=201;".replace(/[^? ]/g, "");
console.log(str); // output : ?
str = str.split('?')[0] // though worked but not readable
I don't want to use for loop for this. Isn't there is any better way to do this?
This should help
var str = "/root/Users?SkillId=201;"
str = str.replace(/\?.*$/g,"");
console.log(str);
Another option is to get the substring before the '?':
str = str.substr(0, str.indexOf('?'));
Match the content before ?
var str = "/root/Users?SkillId=201;"
var a = str.match(/(.*)\?/);
console.log(a[1])
Simply use JavaScript function
var str = "/root/Users?SkillId=201;";
var str = str.substring( 0, str.indexOf("?")-1 );
console.log(str);
here is the fiddle: https://jsfiddle.net/ahmednawazbutt/2fatxLfe/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With