如何从JavaScript中的字符串中提取方括号中的文本?

时间:2016-08-29 00:30:35

标签: javascript

如何从字符串 //header file #ifndef STATISTICIAN_H #define STATISTICIAN_H namespace GREGORY_STOCKER_STATICTICIAN{ class Statistician{ public: Statistician(); void next_number(double); void erase_sequence(); int get_length() const {return length_sequence;} double get_sum() const{return sum;} double get_mean() const; double get_largest() const; double get_smallest() const; double get_last() const; friend Statistician operator + (const Statistician &,const Statistician &); private: int length_sequence; double sum; double smallest; double largest; double last; }; #endif } //implementation file using namespace std; #include "Statictician.h" #include <iostream> #include <cassert> namespace GREGORY_STOCKER_STATICTICIAN{ Statistician :: Statistician() { length_sequence = 0; sum = 0; smallest = 0; largest = 0; last = 0; } void Statistician :: next_number(double num) { length_sequence += 1; sum += num; if(length_sequence == 1) { smallest = num; largest = num; } if (num < smallest) smallest = num; if (num > largest) largest = num; last = num; } void Statistician :: erase_sequence() { length_sequence = 0; sum = 0; smallest =0; largest = 0; last = 0; } double Statistician :: get_mean () const { assert(length_sequence > 0); return sum / 2; } double Statistician :: get_largest() const { assert(length_sequence > 0); return largest; } double Statistician :: get_smallest() const { assert(length_sequence > 0); return smallest; } double Statistician :: get_last() const { assert(length_sequence > 0); return last; } } //the part that is tripping the error message Statistician operator +(const Statistician &s1,const Statistician &s2) { Statistician s3; s3.sum = (s1.sum + s2.sum); s3.sequence_length = (s1.sequence_length + s2.sequence_length; if(s1. largest > s2.largest) s3.largest = s1.largest; else s3.smallest = s2.smallest; if(s1. smallest < s2.smallest) s3.smallest = s1.smallest; else s3.smallest = s2.smallest; s3.last = s2.last; return s3; } 中提取所有方括号对之间的文本,以便我可以获得以下结果:

数组:"[a][b][c][d][e]"

字符串:["a", "b", "c", "d", "e"]

我尝试了以下 "abcde" ,但无济于事:

Regular Expressions

(?<=\[)(.*?)(?=\])

3 个答案:

答案 0 :(得分:1)

<强>研究

在Stack Overflow中搜索后,我只找到了两个解决方案,两个解决方案都使用 \[(.*?)\] ,可以找到它们here

Regular Expressions (1)

  1. (?<=\[)(.*?)(?=\]) :积极的外观。
  2. (?<=\[) :字面匹配字符 \[
  3. [ :匹配除换行符之外的任何字符,并根据需要展开。
  4. (.*?) :积极前瞻。
  5. (?=\]) :从字面上匹配字符 \]

  6. ] (2)

    1. \[(.*?)\] :字面匹配字符 \[
    2. [ :匹配除换行符之外的任何字符,并根据需要展开。
    3. (.*?) :字面匹配字符 \]
    4. 备注:

      (1) 此模式在 ] 中引发错误,因为不支持lookbehind运算符。

      示例:

      JavaScript


      (2) 此模式仅将第一对方括号内的文本作为第二个元素返回。

      示例:

      console.log(/(?<=\[)(.*?)(?=\])/.exec("[a][b][c][d][e]"));
      
      Uncaught SyntaxError: Invalid regular expression: /(?<=\[)(.*?)(?=\])/: Invalid group(…)
      

      解决方案:

      我提出的 console.log(/\[(.*?)\]/.exec("[a][b][c][d][e]")); Returns: ["[a]", "a"] 的最精确解决方案是:

      JavaScript

      现在,根据我们是否希望最终结果是数组或字符串,我们可以这样做:

      var string, array;
      
      string = "[a][b][c][d][e]";
      array = string.split("[");    // → ["", "a]", "b]", "c]", "d]", "e]"]
      string = array1.join("");     // → "a]b]c]d]e]"
      array = string.split("]");    // → ["a", "b", "c", "d", "e", ""]
      

      单线:

      最后,对于像我这样喜欢用最少的代码或我们的 array = array.slice(0, array.length - 1) // → ["a", "b", "c", "d", "e"] /* OR */ string = array.join("") // → "abcde" 家伙来实现最多的人来说,这里有一个方便的单行方式。

      <强>阵列:

      TL;DR

      <强>字符串:

      var a = "[a][b][c][d][e]".split("[").join("").split("]").slice(0,-1);
      /* OR */
      var a = "[a][b][c][d][e]".slice(1,-1).split(']['); // Thanks @xorspark
      

答案 1 :(得分:0)

我不知道你在那个阵列中期待什么文本,但是对于你给出的例子。

var arrStr = "[a][b][c][d][e]";
var arr = arrStr.match(/[a-z]/g) --> [ 'a', 'b', 'c', 'd', 'e' ] with typeof 'array'

then you can just use `.concat()` on the produced array to combine them into a string.

如果您希望方括号之间有多个字符,则正则表达式可以(/[a-z]+/g)或根据您的喜好进行调整。

答案 2 :(得分:0)

我认为这种方法对你很有意思。

&#13;
&#13;
    var arr = [];
    var str = '';
    var input = "[a][b][c][d][e]";
    input.replace(/\[(.*?)\]/g, function(match, pattern){
      arr.push(pattern);
      str += pattern;
      return match;//just in case ;)
    });
    
    console.log('Arr:', arr);
    console.log('String:', str);

//And trivial solution if you need only string
var a = input.replace(/\[|\]/g, '');
console.log('Trivial:',a);
&#13;
&#13;
&#13;