Skip to content Skip to sidebar Skip to footer

Removing A String Within A String Using JavaScript

I am having a string like this: 'welcome country ! and some texts Keyword1:the value keyword2: the value2' I want to remove keyword on undo the corresponding checkbox and also it

Solution 1:

if you want to remove 'Keyword1:the value', then try

var keyWordToRemove = 'Keyword1';
var rgxStr = keyWordToRemove + ':[a-zA-Z0-9 ]*\n';
var rgx = new RegExp(rgxStr,'g');
var text = `welcome country !
and some texts

Keyword1:the value
keyword2: the value2`;

console.log(text);

text = text.replace(rgx,"");

console.log(text);

Hope it helps :)


Solution 2:

You can try it using regex like this

var url = `welcome country !
and some texts

Keyword1:the value
keyword2: the value2`;

console.log(url.replace("Keyword1:", "test key "))
console.log(url.replace(/(Keyword1)(\:(.*))/, "$1 test value"))

you can replace Keyword1 with $(this).attr("data-id") + ":" in your code


Post a Comment for "Removing A String Within A String Using JavaScript"