0

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 its value using Javascript. Now i could remove the keyword while undo checkbox but not the value they have entered near the keyword. I have tried substring functions and some other, but i couldn't fix it.

my code below:

$("#txtNote").val(url.replace($(this).attr("data-id") + ":", ""));

I just want to remove the texts immediately after the ":"

here is my entire code:

 if ($(this).attr("data-selected1") == "true") {
            $("#detailChronic").show();         
            $(this).attr("data-selected1", "false");
           //$(".hjk").remove(":contains('" + $(this).attr("data-id") + "')");
            var url = $.trim($("#txtNote").val());
            str = $("#txtNote").val();
            //var t = str.substring(str.indexOf(":"))
            //alert(t);
            //url = url.replace(/\s+/g, "\n");

            // $("#txtNote").val(url.replace($(this).attr("data-id") + ":", ""));
           // $("#txtNote").val(url.replace($(this).attr("data-id") + ":" + $(this).attr("data-id").value(), ""));             
            //url.replace($(this).attr("data-id") + ":", "");
            alert(url);
            var temp2 = temp1.replace(/($(this).attr("data-id"))(\:(.*))/, "");
            alert(temp2);
            var temp1 = url.replace($(this).attr("data-id"), "");

            alert(temp1);
            $("#txtNote").val(temp1);

           // $("#txtNote").val(url.replace($(this).attr("data-id") + ":" + $(this).attr("data-id").value(), ""));

                if ($("#selectedList").html() == "") {
                $("#detailChronic").hide();
            }
        }
3
  • Please create a relevant snippet using <> in the editor. Commented Jan 15, 2018 at 9:53
  • Will all of your keywords be in separate line? Commented Jan 15, 2018 at 10:02
  • @leninhasda , now it is in separate line. can we remove the particular line from the string ? Commented Jan 15, 2018 at 10:10

2 Answers 2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.