var mystr = ("data-123").slice(5); // This just defines a start point so the output is "123"var mystr = ("data-123").slice(5,7); // This defines a start and an end so the output is "12"
String.prototype.deleteWord = function (searchTerm) {var str = this;var n = str.search(searchTerm);while (str.search(searchTerm) > -1) {n = str.search(searchTerm);str = str.substring(0, n) + str.substring(n + searchTerm.length, str.length);}return str;}
// Use it like this:var string = "text is the cool!!";string.deleteWord('the'); // Returns text is cool!!
//Example coutesy of MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAllconst p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
const regex = /dog/gi;
try {console.log(p.replaceAll(regex, 'ferret'));// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
console.log(p.replaceAll('dog', 'monkey'));// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"console.log('Your browser is supported!');} catch (e) {console.log('Your browser is unsupported! :(');}
Another possible approach would be to add a method to the String prototype as follows:
String.prototype.remove = function (s){return this.replace(s,'')}
// After that it will be used like this:
a = 'ktkhkiksk kiksk ktkhkek kcklkekaknk kmkekskskakgkekk';a = a.remove('k');console.log(a);
String.prototype.removeAll = function (s){return this.replaceAll(s,'')}
// After that it will be used like this:
a = 'ktkhkiksk kiksk ktkhkek kcklkekaknk kmkekskskakgkekk';a = a.removeAll('k');console.log(a);
function strRemoveAll(s,r){return s.replaceAll(r,'');}
// you can use it as:
let a = 'ktkhkiksk kiksk ktkhkek kcklkekaknk kmkekskskakgkekk'b = strRemoveAll (a,'k');console.log(b);