2

I have a string in the form of [stringKey1:stringValue1][stringKey2:stringValue2][stringKey3:stringValue3].

I'm having trouble using regex to remove the the string between [] using a stringKey.

What Im trying to do is

const string = "[stringKey1:stringValue1][stringKey2:stringValue2][stringKey3:stringValue3]";

const newString = removeStringKey("stringKey2");

newString === "[stringKey1:stringValue1][stringKey3:stringValue3]"

Any suggestions?

0

1 Answer 1

3

Use a regex replacement:

function removeStringKey(key, input) {
    var re = new RegExp("\\[" + key + ":.*?\\]");
    return input.replace(re, "");
}

const string = "[stringKey1:stringValue1][stringKey2:stringValue2][stringKey3:stringValue3]";
console.log(removeStringKey("stringKey2", string));
 

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

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.