2016-07-02 7 views
0

文字列からいくつかの文字を削除するための関数をJavaスクリプトで作成していますstring.slice()を試しました string.substr() string.substring()コロン文字列から文字範囲を削除するJavascript

は、その人の名前の長さは、私が

のようになりたい

これはテストテキスト1

でいただきまし決めていません

これはこれは、これはいくつかの時間のテキストはのようなものですテストtext4

でテストテキスト3

でテストテキスト2

です(のQt:ジョン:私が言った....彼女:いない再び)この場合、私は結果が(私は言ったとき....)彼女は:再びない) 私はちょうど(Qt:name :)それを後に書かれたものを削除したいと思います。

function myFunction() { 
    var a="Qt: joe: this is test text1"; 
    var b="Qt: bella: this is test text2"; 
    var c="this is test text3"; 
    var d="Qt: alex: this is test text4"; 
    removetxt(a); 
    removetxt(b); 
    removetxt(c); 
    removetxt(d); 
} 

function removetxt(x){ 
    if (x.slice(0,2) == 'Qt'){ 
    console.log("found Qt"); 
    }else 
    console.log(x);  
} 
+0

正規表現を使用し、 'x.replace(/^.+ようなもの:?/ gで、 "" ) ' – ftor

+0

私はそれを試しましたが、いくつかの時間のテキストは次のようなものです(Qt:john:私が言ったとき....彼女:私はこの正規表現を使用する場合、今私は私が言うと思っているものを得たことを願って(再びではない)だけを得るでしょう – faysal

+0

あなたの質問は意味をなさない。文字列から文字を削除するか、部分文字列を検索しますか? – ftor

答えて

2
function myFunction() { 
    var a="Qt: joe: this is test text1"; 
    var b="Qt: bella: this is test text2"; 
    var c="this is test text3"; 
    var d="Qt: alex: this is test text4"; 
    removetxt(a); 
    removetxt(b); 
    removetxt(c); 
    removetxt(d); 
} 

function removetxt(x){ 
    var y = x.split(':'); 

    if (y[0] == 'Qt'){ 
    console.log("found Qt"); 
    }else 
    console.log(y);  
} 
0

これを試してみてください:

Replace(a, "Qt:", ""); 

結果は次のとおりです:

joe: this is test text1 
+0

しかし、私はその名前も削除したいと思っています。(Qt:name :)私が望むのはこれ以後に書かれたものです。 – faysal

0
function myFunction() { 
    var a="Qt: joe: this is test text1"; 
    var b="Qt: bella: this is test text2"; 
    var c="this is test text3"; 
    var d="Qt: alex: this is test text4"; 

    extractTextAfterLastColon(a); 
    extractTextAfterLastColon(b); 
    extractTextAfterLastColon(c); 
    extractTextAfterLastColon(d); 
} 

function extractTextAfterLastColon(str){ 
    var // any non colon character sequence that reaches the end. 
     regX = (/([^:]+)$/), 
     result = regX.exec(str), 

     text = result && result[1].trim(); 

    console.log(text);  
} 

myFunction(); 

function Replace(str, find, replaceWith) { 
if (find === replaceWith) 
    return str; 
do 
{ 
    str = str.replace(find, replaceWith); 
} 
while(str.indexOf(find) !== -1); 
return str; 
} 

var a="Qt: joe: this is test text1"; 
var qt = "Qt:"; 

は今、このような関数を呼び出します

OP:私は私の質問を更新し、より詳細に私の問題を説明する仲間、あなたの関数はありません同じ私はx.replaceから得ることができるもの(/^.+:?/ gで、 "")

これで新しい視点が得られます。ソリューションは同じですが、RegExpはより複雑になります。

function myFunction() { 
    var a="Qt: joe: this is test text1"; 
    var b="Qt: bella: this is test text2"; 
    var c="this is test text3"; 
    var d="Qt: alex: this is test text4"; 
    var e="Qt: john: when i said .... she: not again"; 

    extractTextAfterQuestionOrProtagonist(a); 
    extractTextAfterQuestionOrProtagonist(b); 
    extractTextAfterQuestionOrProtagonist(c); 
    extractTextAfterQuestionOrProtagonist(d); 
    extractTextAfterQuestionOrProtagonist(e); 
} 

function extractTextAfterQuestionOrProtagonist(str) { 
    var 
     regX = (/(?:qt\:\s*(?:[^\s:]+:\s*)*)(.*)$/i), 
     result = regX.exec(str), 

     text = ((result && result[1]) || str).trim(); 

    console.log(text);  
} 

myFunction(); 
+0

私の質問を更新し、もっと詳しく、私の問題を説明します。 .replace(/^.+:?/ g、 "") ' – faysal

0

これを試してみてください。

$(function(){ 
 
    var a="Qt: joe: this is test text1"; 
 
    var b="Qt: bella: this is test text2"; 
 
    var c="this is test text3"; 
 
    var d="Qt: alex: this is test text4"; 
 
    removetxt(a); 
 
    removetxt(b); 
 
    removetxt(c); 
 
    removetxt(d); 
 
}); 
 

 
function removetxt(x){ 
 
    var y = x.split(':'); 
 

 
    if (y.length > 1){ 
 
    alert(y[y.length-1]) 
 
    }else 
 
    alert(y[0]);  
 
}

0

このヘルプを使用するには:

<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
    </head> 
 
    <body> 
 
     <script> 
 
      myFunction() 
 
      function myFunction() { 
 
       var a="Qt: joe: this is test text1"; 
 
       var b="Qt: bella: this is test text2"; 
 
       var c="this is test text3"; 
 
       var d="Qt: alex: this is test text4"; 
 
       removetxt(a); 
 
       removetxt(b); 
 
       removetxt(c); 
 
       removetxt(d); 
 
      } 
 
      function removetxt(x) { 
 
       var x1 = x.replace(/^(Qt:.*:)/g,""); 
 
       alert(x1); 
 
      } 
 

 
     </script> 
 
    </body> 
 
</html>

0
あなたは、少なくとも二つのコロンまたはnoneを保証することができれば、次のように動作しますあなたの例から

、:

var temp=yourStr.substr(yourStr.indexOf(":")+1); 
//if indexOf=-1, whole str returned 
temp=temp.substr(temp.indexOf(":")+1); 
var yourNewStr=temp; 
関連する問題