2017-06-10 6 views
2

に空白でパディングそのURLのすべてのエントリを削除します。これを行うには、最初に正規表現が必要です。私は最初のオカレンスを見つける必要があり、見つけたいだけであることに注意してください。は、私は次のように見えることができますエントリを持つことになりますHTMLのテキストエリアを持っているテキストエリア

この機能では完全一致を削除する必要があります。それは

DeleteEntry("youtube.com"); 

2行目を削除するべきではありませんが、

DeleteEntry("youtube.com word word"); 

はべきです。

だから基本的に、私はこれは私が持っているものである

(beginningOfString OR newlineChar) then (anyWhiteSpaceExceptNewline) then (ENTRY) then (anyWhiteSpaceExceptNewline) then (endOfString OR newlineChar) 

このパターンに一致する必要があり、これまで

var expression = "\n|^[ \f\r\t\v]*" + entry + "[ \f\r\t\v]*\n|$"; 
var match = listbox.value.match(expression); 
私が期待していように動作するようには思えない

それに。

+0

あなたがこれまで何をしている私たちを見ることができます。 –

+0

私は既に質問の末尾にある... –

+0

あなたは 'm'フラグで試してみることができます - var式= new RegExp("^[\ f \ r \ t \ v] * "+ entry +" [ \ f \ r \ t \ v] * $ "、" m "); ' –

答えて

1

注:\を文字列の内側に使用する場合は、エスケープする必要があります。 "\some text"が間違っていますが、"\\some text"が正しいです。

var ta = document.getElementById("ta"), 
 
    inp = document.getElementById("inp"), 
 
    btn = document.getElementById("btn"); 
 
    
 
// escape text to be used inside RegeExp (from: https://stackoverflow.com/q/3115150/6647153) 
 
function escape(text) { 
 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); 
 
} 
 

 
function deleteEntry(query) { 
 
    var text = ta.value; 
 
     
 
    var regexText = query.trim()         // remove surrounding spaces 
 
         .split(/\s+/)        // split into tokens ("a b c" becomes ["a", "b", "c"]) 
 
         .map(escape)        // escape the tokens ("a.com" becomes "a\\.c" so it'll be /a\.c/ where the '.' is regarded as litteral '.' not as the special character .) 
 
         .join("\\s+");        // joins the tokens together with \s+ (["a", "b", "c"] becomes "a\\s+b\\s+c" so it'll be /a\s+b\s+c/) 
 
     
 
    var regex = new RegExp("^\\s*" + regexText + "\\s*$", "gm"); // surrond regexText with ^\s* and \s*$ and use the g modifier for multiple matches and the m modifier for multiline text 
 
    
 
    ta.value = text.replace(regex, "");        // replace the matched text with "" and reassign it back to the textarea 
 
} 
 
    
 
btn.onclick = function() { 
 
    deleteEntry(inp.value);           // calling deleteEntry passing to it the input's value as the query 
 
}
textarea { 
 
    display: block; 
 
    width: 100%; 
 
    height: 100px; 
 
}
<textarea id="ta"> 
 
google.com 
 
    youtube.com word word 
 
    netflix.com 
 
twitch.tv 
 
    vimeo.com word 
 
soundcloud.com word word 
 
</textarea> 
 
<input id="inp"><button id="btn">Delete</button>

+0

私は、複数行のオプションを使うことです。私のオリジナルの正規表現は、次のようにいくつかの追加のかっこで処理されているようです: ''(^ | \ n)[+ f \ r \ t \ v] * "+エントリ+" [\ f \ r \ t \ v] *(\ n | $) "'なぜ括弧が働くのか分かりませんが、期待された動作を得ました。 –

+0

さらに、一致を選択して削除コマンドを実行して、ブラウザの元に戻す/やり直しスタックに削除が追加されるようにするために、リスト文字列の開始点と終了点のインデックスを取得する必要がありました。だから、 'replace'を使う代わりに' match() 'と' indexOf() 'と' match.length'を使って開始インデックスと終了インデックスを取得しました。上記のコメントで私の代替非複数行アプローチを使用すると、1つの 'match()'呼び出しから一致文字列、長さの一致、および開始インデックスをすべて取得できます。 –

+0

この回答には、最初のURLから特別な正規表現の文字をエスケープすることも含まれていますが、私はこのオリジナルの投稿では言及していませんでした。これを必ず実行してください。 –

関連する問題