I have strings like these
total sales 234 rs
total cost 651 rs
and I want to get only
end result should look like this
total sales
total cost
how can i get that please help thanks
CodePudding user response:
Assuming you always want to cut it off just before the first number, you could find the index of the first number:
const index = 'total sales 234 rs'.search(/\d/);
And then just get the substring:
substring(0, index - 1);
CodePudding user response:
You can do something like this:
let str = 'total sales 234 rs';
let result = str.split(' ').slice(0,2).join(' ');
CodePudding user response:
Assuming that the string can contain more than two main words
For eg Total goods cost 200 rs
then use the below function
function reducer(str) { // where str is the initial string
const modifiedStr = str.split(' ').reduce((acc, substr) => {
if(!parseInt(substr, 10) && substr !== 'rs') acc =substr ' ';
return acc;
}, "");
return modifiedStr.slice(0,modifiedStr.length - 2);
}