Skip to content

字符串解析问题

Posted on:2021年7月7日 at 00:13

实现函数使得将str字符串中的{}内的变量替换,如果属性不存在保持原样(比如{a.d})

var a = {
  b: 123,
  c: "456",
  e: "789",
};
var str = `a{a.b}aa{a.c}aa {a.d}aaaa`;
// => 'a123aa456aa {a.d}aaaa'

const fn1 = (str, obj) => {
  let res = "";
  // 标志位,标志前面是否有{
  let flag = false;
  let start;
  for (let i = 0; i < str.length; i++) {
    if (str[i] === "{") {
      flag = true;
      start = i + 1;
      continue;
    }
    if (!flag) res += str[i];
    else {
      if (str[i] === "}") {
        flag = false;
        res += match(str.slice(start, i), obj);
      }
    }
  }
  return res;
};
// 对象匹配操作
const match = (str, obj) => {
  const keys = str.split(".").slice(1);
  let index = 0;
  let o = obj;
  while (index < keys.length) {
    const key = keys[index];
    if (!o[key]) {
      return `{${str}}`;
    } else {
      o = o[key];
    }
    index++;
  }
  return o;
};
原文转自:https://fe.ecool.fun/topic/7375c380-9dd8-497e-9b74-e4cc0779532c