Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing special words from Delimitted string

Ive a situation to remove some words from a delimitted string in which the last char is ¶.

That means that if the string is:

keyword1,keyword2,keyword3¶,keyword4,keyword5¶,keyword6

The output string should be:

keyword1,keyword2,keyword4,keyword6

How can we achieve that in javascript?

This is what i did but i would like to do it without looping:

var s='keyword1,keyword2,keyword3¶,keyword4,keyword5¶,keyword6';
s=s.split(',');
var t=[];
$(s).each(function(index,element){
var lastchar=element[element.length-1];
if(lastchar!='¶')
{
t.push(element);
}
});
console.info(t.join(','));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
like image 511
Sandeep Thomas Avatar asked Dec 31 '25 15:12

Sandeep Thomas


2 Answers

Problem can be solved using regular expressions:

var s='keyword1,keyword2,keyword3¶,keyword4,keyword5¶,keyword6';
s=s.replace(/,keyword\d+¶/g, '');
console.info(s);
like image 189
ollazarev Avatar answered Jan 03 '26 03:01

ollazarev


You should use the filter functionality in the JS.

 var _s = "keyword1,keyword2,keyword3¶,keyword4,keyword5¶,keyword6";
 
 var _output = _s.split(",").filter(function(word){
    return (word[word.length - 1] !== "¶");
 }).join(",");
 
 console.log(_output);
   
like image 26
Thusitha Avatar answered Jan 03 '26 05:01

Thusitha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!