从字符串Javascript中删除字符范围

时间:2016-07-02 12:56:03

标签: javascript string function

我在java脚本中创建一个函数来从字符串中删除一些字符我试过string.slice() 使用String.substr() string.substring()但我想 在冒号

之前删除所有内容

它没有决定人名的长度

我想要像

这样的结果

这是测试文本1

这是测试文本2

这是测试文本3

这是测试文本4

有些时候文字就像( 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);      
}

6 个答案:

答案 0 :(得分: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);      
}

答案 1 :(得分:0)

试试这个:

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:";

现在调用这个函数:

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

结果是:

joe: this is test text1

答案 2 :(得分: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();
  

OP:兄弟我更新了我的问题并更详细地解释了我的问题,你的功能与我从x.replace得到的相同(/^.+:?/ g,&#34;&#34;)< / p>

现在它给它带来了新的视角 - 然后解决方案是相同的,但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();

答案 3 :(得分: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]);      
}

答案 4 :(得分:0)

这有助于您:

   btnAdd.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        // show input dialog
        addtoCart();
       startActivity(new Intent(getApplicationContext(),CartActivity.class));

    }
});

}

答案 5 :(得分:0)

从您的示例中,如果您可以保证至少两个冒号或没有冒号,则以下内容将起作用:

var temp=yourStr.substr(yourStr.indexOf(":")+1);
 //if indexOf=-1, whole str returned
temp=temp.substr(temp.indexOf(":")+1);
var yourNewStr=temp;