2017-01-10 16 views
-3

を削除:Javascriptが、私は次の形式の文字列を持っているサブ

[text text] Text to stay 

をそして、私は最終的に文字列がされるように、ブラケットと内のテキストを削除する:

Text to Stay 

されていますそれは正規表現を使用してこれを達成する可能性があり、関数は置き換えますか?

+1

おそらくよりフィッティング重複:https://stackoverflow.com/questions/4292468/javascript-regex-remove-text-between-brackets –

答えて

4

はい、あなたはそれは次のように、機能や正規表現を交換して行うことができます。

var text = "[text text] Text to stay"; 
var replaced = text.replace(/\[(.*)\]/, ""); 

このスニペットをチェックしてください:

var text = "[text text] Text to stay"; 
 
var replaced = text.replace(/\[(.*)\]/, ""); 
 

 
document.write(replaced);

+0

** '" [テキストを回すでしょうテキスト]テキストを「**」だけ**「そのまま」**「滞在」する**。代わりに、貪欲でないマッチングを使用する必要があります。 –

1

あなたはこの短い手を試すことができます。

var text = "[text text] Text to stay"; 
 
var updatedText = text.split('] ')[1];

1

var str = "[text text] Text to stay"; 
 
alert(str.replace(str.substr(str.indexOf('['),str.indexOf(']') - str.indexOf('[') + 1),""));

1

テキストが]を含む場合、この正規表現にも動作します。

var text = "[text text] Text to stay"; 
 
var replaced = text.replace(/\[.*?\] /, ""); 
 

 
document.write(replaced);

関連する問題