#101 解析字串


  • 0

    const extractStr = (str) => {
    var arr = str.split("");
    var crr = [];
    var func = function(){
    if(arr.indexOf(":")!==-1&&arr.indexOf(".")!==-1){
    if(arr.indexOf(":")<arr.indexOf(".")){
    var brr = arr.slice(arr.indexOf(":")+1,arr.indexOf("."));
    brr.splice(0,brr.lastIndexOf(":")+1);
    crr.push(brr.join(""));
    }
    arr.splice(0,arr.indexOf(".")+1);
    func()
    }
    }
    func()
    return crr;
    }


  • 0

    代码略长,但是是比较传统的字符串解析方式做的。
    因为篇幅,所以没用状态机,若用了丑陋了 if / else

    const extractStr = str => {
        const TOKEN_START = "S";
        const TOKEN_END = "E";
        const TOKEN_OTHER = "O";
        const STATUS_NOT_KEY_WORDS = "NK";
        const STATUS_KEY_WORKDS = "K";
        let i = 0;
        let tokens = [];
        while (i < str.length) {
            let c = str[i];
            if (c === ":")
                tokens.push({
                    type: TOKEN_START
                });
            else if (c === ".")
                tokens.push({
                    type: TOKEN_END
                });
            else
                tokens.push({
                    type: TOKEN_OTHER,
                    value: c
                });
            i++;
        }
        let result = [];
        let item = [];
        let token;
        i = 0;
        let status = STATUS_NOT_KEY_WORDS;
        while (i < tokens.length) {
            token = tokens[i++];
            if (token.type === TOKEN_START) {
                if (status === STATUS_NOT_KEY_WORDS) {
                    status = STATUS_KEY_WORKDS;
                    continue;
                }
                if (item.length > 0) {
                    item = [];
                }
            }
            if (token.type === TOKEN_OTHER && status === STATUS_KEY_WORKDS) {
                item.push(token.value);
                continue;
            }
            if (token.type === TOKEN_END && status === STATUS_KEY_WORKDS) {
                result.push(item.join(""));
                item = [];
                status = STATUS_NOT_KEY_WORDS;
                continue;
            }
        }
        return result;
    };
    

  • 0

    const extractStr = (str) => /* TODO */
    {
    let arr = [],flag = 0,temp = ''
    for (let i = 0; i < str.length; i++) {
    if (flag == 0) {
    if(str[i] == ':') {
    flag = 1
    }
    } else if (flag == 1) {
    if (str[i]=='.') {
    flag = 0
    arr.push(temp)
    temp = ''
    }
    if (str[i] == ':') {
    temp = ''
    }
    if (str[i] != ':' && str[i] != '.') {
    temp += str[i]
    }
    }

    }
    return arr
    }


  • 0

    const extractStr = (str) => {
      const reg = /:([^:.]*)\./g;
      const ary = [];
      let result = reg.exec(str);
      while(result){
        ary.push(result[1]);
        result = reg.exec(str);
      }
      return ary;
    }
    

  • 0

    const extractStr = str => (res => res ? res.map(str => str.slice(1, -1)) : [])(str.match(/:[^:]*?\./g));
    

    正则不给向前断言真是太*了……


  • 0

    @张夜白#101 解析字串 中说:

    用C++/Java的思路和写法写的,居然被AC了,从来没写过JS,大神给看一哈。

    const extractStr = (str) => /* TODO */
    {
        let result = [];
        let targets = str.split(".").slice(0, -1);
        targets.forEach(target=>{
            let position = target.lastIndexOf(":");
            if( position >= 0){
                result.push(target.substring(position + 1));
            }
        });
        return result;
    }
    

  • 0

    @chishengqi 之前看exec属性介绍看的一头雾水,看过你的会用了。


  • 0

    //可行
    const extractStr = (str) => {
    const ret = str.match(/:([^:.])*?./g) || []
    return ret.map((subStr) => subStr.replace(/[:.]/g, ''))
    }


  • 0

    const extractStr = (str) => {
    return str.split(':')
    .map(item => item.split('.'))
    .filter((item, idx) => idx > 0 && item.length > 1)
    .map(item => item[0])
    }


  • 0

    const extractStr = (str) => {
      let arr = [], arr1 = str.split(':');
      arr1.forEach((item, i) => {
        if (i !==0 && item.length !== 0) {
          let index = item.indexOf('.');
          if (index !== -1) {
            arr.push(item.slice(0, index));
          }
        }
      })
      return arr;
    }

  • 0

    const extractStr = (str) => {
    let targetArr = str.split(':');
    return targetArr.map((itemStr, index) => {
    if(index === 0 && itemStr !== '') return null;
    let pointPosition = itemStr.indexOf('.');
    if(pointPosition !== -1) {
    return itemStr.slice(0, pointPosition);
    }
    return null;
    }).filter(s => s !== null);
    };


  • 0

    const extractStr = (str) => {
      let targetArr = str.split(':');
      return targetArr.map((itemStr, index) => {
        if(index === 0 && itemStr !== '') return null;
        let pointPosition = itemStr.indexOf('.');
        if(pointPosition !== -1) {
          return itemStr.slice(0, pointPosition);
        } 
        return null;
      }).filter(s => s !== null);
    };
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待