Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在js中通过匹配字符对字符串和单词进行条件拆分_Javascript_String - Fatal编程技术网

Javascript 在js中通过匹配字符对字符串和单词进行条件拆分

Javascript 在js中通过匹配字符对字符串和单词进行条件拆分,javascript,string,Javascript,String,我有一个很长的字符串,其中包含html作为字符串、数字、我的特殊绑定和数字。我想用空格分开我的绑定和句子,但我的程序将绑定和单词分开 我的js代码:- var x = 'hey this is {{name}} and I love to {{write}} and to learn as much as I can. Now I am trying to separate sentences and my bindings' var c = x.match(/\s*\S*\s*/

我有一个很长的字符串,其中包含html作为字符串、数字、我的特殊绑定和数字。我想用空格分开我的绑定和句子,但我的程序将绑定和单词分开

我的js代码:-

var x = 'hey this is {{name}}    and I love to  {{write}} and to learn as 
much as I can. Now I am trying to separate sentences and my bindings'

var c  = x.match(/\s*\S*\s*/g) // this splits words from string including 
   space

var mt = x.match(/{(.*)}/g); // trying to  take out bindings but this don't 
 work

mt.forEach(function(a){     // taking each bindings separately 
  var z = x.match(a) 
})

console.log(mt)
像这样的事。。但我知道这是完全错误的请帮帮我 我不知道:-

我期望的输出:-

(5) ["hey this is", "i", "{{name}}", "    and I love to  ", "{{write}}", " and to learn as ↵ much as I can. Now I am trying to separate sentences and my bindings"]
我该怎么做

请不要使用jquery

试试这个:
我已经对我的代码进行了注释,希望它能更容易阅读但请注意,尽管此代码解决了您的问题,但它还远远不够完美。

var rawString = 'hey this is {{name}}    and I love to  {{write}} and to learn as much as I can. Now I am trying to separate sentences and my bindings';
var arrayRawString  = rawString.match(/\s*\S*\s*/g); // this splits words from string including space
var arrayPlaceholder = rawString.match(/{(.\S*)}+/g); // trying to  take out bindings but this don't work

// to store the final output
var separedArray = [];

// keeping track of the index to stich the array up
var posStart = 0;
var posEnd = 0;

arrayPlaceholder.forEach(function(arg){     // taking each bindings separately 

  // length of the array that holds placeholder (bindings)
  var arsLength = arrayRawString.length;

  for(var i = 0; i < arsLength; ++i) { 

    // if the provided text matches the original array's element
    if(arrayRawString[i].match(arg)){

      // to store the index
      posEnd = arrayRawString.indexOf(arrayRawString[i]);

      // join the pieces together upto the index defined
      var res = arrayRawString.slice(posStart, posEnd).join('');

      // to indicate whether the stored string is the placeholder
      var flag = true;

      // store the string obtained
      separedArray.push(res.replace(arrayPlaceholder[(arrayPlaceholder.indexOf(arg) - 1) < 0 ? 0 : arrayPlaceholder.indexOf(arg) - 1 ], ''));

      // check if the string still has placeholder (bindings)
      // to remove it
      for(var j = 0; j < arg.length; ++j) {
          if(res[j] !== arg[j]) {
            flag = !flag;
          }
      }

      if ( flag ) {
        separedArray.push(arg);
      }

      // last end position is the start position for next round
      posStart = posEnd;

      // because the loop runs only arrayPlaceholder.length times
      // it solves the problem of last part not getting pushed to the final array
      if( arrayPlaceholder[arrayPlaceholder.length-1] === arg ) {
        res = arrayRawString.slice(posStart, arrayRawString.length).join('');
        separedArray.push(res.replace(arg, ''));
      }
    }
  }

});

console.log(separedArray);
var rawString='嘿,这是{{name}},我喜欢{{write}}并尽可能多地学习。现在我试着把句子和绑定分开;
var arrayRawString=rawString.match(/\s*\s*\s*/g);//这将从包含空格的字符串中拆分单词
var arrayPlaceholder=rawString.match(/{(.\S*)}+/g);//试图取出绑定,但这不起作用
//存储最终输出
var separedArray=[];
//跟踪索引以粘贴数组
var-posStart=0;
var-posEnd=0;
forEach(函数(arg){//分别接受每个绑定
//包含占位符(绑定)的数组的长度
var arsLength=arrayRawString.length;
对于(var i=0;i