I have a string of the following format:
[text text] Text to stay
And I want to remove the brackets and the text inside, so that the final string will be:
Text to Stay
Is it possible to achieve this using regex and the function replace?
I have a string of the following format:
[text text] Text to stay
And I want to remove the brackets and the text inside, so that the final string will be:
Text to Stay
Is it possible to achieve this using regex and the function replace?
Yes, you can do it with replace function and regexp, as follow:
var text = "[text text] Text to stay";
var replaced = text.replace(/\[(.*)\]/, "");
Check this snippet:
var text = "[text text] Text to stay";
var replaced = text.replace(/\[(.*)\]/, "");
document.write(replaced);
var str = "[text text] Text to stay";
alert(str.replace(str.substr(str.indexOf('['),str.indexOf(']') - str.indexOf('[') + 1),""));
You can try this short hand.
var text = "[text text] Text to stay";
var updatedText = text.split('] ')[1];
This regex also works when the text includes ]
.
var text = "[text text] Text to stay";
var replaced = text.replace(/\[.*?\] /, "");
document.write(replaced);