【问题标题】:spliting text into array in javascript在javascript中将文本拆分为数组
【发布时间】:2016-12-11 20:35:17
【问题描述】:

我想在 javascript 文本中获取 ,. 时进行拆分。

我的文字是这样的:

猫爬上了高大的树。在这句话中,$U_SEL{} 是一个名词。

我想要数组为:

1.The cats climbed the tall tree.
2.In this sentence
3.$U_SEL{}
4.is a noun

【问题讨论】:

  • 你为什么要在 $U_SEL{}is a noun. 之间分开 - 没有 , 或 .那里....split(/[.,]/)
  • 如果你想隔离 $U_SEL{} 你需要考虑如何识别字符串的那部分。例如,如果您首先拆分 , 。然后搜索包含以 $ 开头的变量的字符串,然后将该字符串拆分为空格......

标签: javascript


【解决方案1】:

这里是regex。要在 {} 上拆分,首先将 that 替换为 {},{}.,然后尝试拆分。

var str = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun";
str = str.replace("{}", "{},");

//Array with splitted value
var result = str.split(/[,.]/g);

//Printing the result array
console.log(result);

【讨论】:

    【解决方案2】:

    可能拆分不准确,因为拆分需要单个字符分隔符,并且第三个元素没有分隔符。

    尝试捕获而不是拆分可能效果更好(虽然我不知道从性能的角度来看这是否明智)。

    你可以试试这个:

    var pattern = /(([^.,]+?)([.,]|\{\})) */g;
    
    var captures = [];
    var s = 'First capture.Second capture, $THIRD_CAPTURE{} fourth capture.';
    while ( (match = pattern.exec(s)) != null ) {
    	if (match[3] == "." || match[3] == ",") {
    		captures.push(match[2]);
    	} else {
    		captures.push(match[1]);
    	}
    }
    console.log(captures);
    
    var captures = [];
    var s = 'The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.';
    while ( (match = pattern.exec(s)) != null ) {
    	if (match[3] == "." || match[3] == ",") {
    		captures.push(match[2]);
    	} else {
    		captures.push(match[1]);
    	}
    }
    console.log(captures);

    原理如下。

    • 捕获以点或逗号结尾的句子部分的块,不带内点或逗号,或以空括号对结尾
    • 在每个块中,捕获内容和结尾(点、逗号或空括号对)

    对于每个结果匹配,您有三个捕获:

    • 在索引 1 处,第一个块
    • 在索引 3 处,结尾
    • 在索引 2 处,没有结尾的内容

    然后,根据结局,存储idx 1或2的匹配。

    您可以修改循环选择匹配项以获得您想要的结果,在第一次捕获而不是在最后一个捕获点上,除非它是一个错字。

    【讨论】:

      【解决方案3】:

      这个挑战的正则表达式是。

      var text = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun."
      var regex = /[.,]/;
      text.split(regex);
      

      有关regex的更多信息,请访问 https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

      【讨论】:

        【解决方案4】:

        在这种情况下,正则表达式是您的最佳选择。以上所有帖子都已经正确涵盖了解决您问题的这种方式。如果您不知道正则表达式的工作原理,我将在这里留下另一种方法,该方法将提供您所追求的。

        尽管在您的场景中,RegExp 是最佳选择。上面的代码主要是为了展示如何在不使用 RegExps 的情况下完成它。 (更不用说添加更多分隔符会变得混乱)

        var myString = "The cats climbed the tall tree.In this sentence, $U_SEL{} , is a noun";
        var mySplitString = myString.split(",");
        var myFinalArray = new Array();
        
        mySplitString.forEach(function(part){
          var myTemp = part.split(".");
          myTemp.forEach(function(key){
            myFinalArray.push(key);
          });
        });
        
        console.log(myFinalArray);

        【讨论】:

          【解决方案5】:

          试试这个

          <script type="text/javascript">
              var text = "The cats climbed the tall tree.In this sentence, $U_SEL{}, is a noun";
              var spliteds = text.split(/[\.,]/);
          
              alert(spliteds[0]);
              alert(spliteds[1]);
              alert(spliteds[2]);
              alert(spliteds[3]);
          </script>
          

          【讨论】:

          • {} 后面没有,。再次检查问题!
          【解决方案6】:
           'The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.'.split(/[\.,]/)
          

          会返回:

          Array [ "The cats climbed the tall tree", "In this sentence", " $U_SEL{} is a noun", "" ]
          

          看看String.prototype.split()

          【讨论】:

          猜你喜欢
          • 2013-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-10
          相关资源
          最近更新 更多