:PrismicReplace

问题描述 投票:0回答:1

我有以下几点 Arraystrings:

array = [
   "My Website | :PrismicReplace|title"
   "My Website | Contact Us"
];

我想在这些数组中循环,如果元素中含有 :PrismicReplace 我想提取管子后面的值,如 :PrismicReplace|title 字符串内的内容返回 title 作为关键...

即:

>> my_function("My Website | :PrismicReplace|title")
title

字符串可以是

"My Website | :PrismicReplace|someReallyReallyReallyLongParameter"

(返回一些ReallyReallyReallyLongParameter)

"My Website | :PrismicReplace|someReallyReallyReallyLongParameter Some Other Stuff"

(仍然返回一些ReallyReallyLongParameter)

我尝试了循环,和regex匹配的组合,但还没有达到我的目的......但我在想,希望有快速解决这个问题的方法?目前我最好的办法是这样的。

    if (new RegExp(':PrismicReplace').test(tagDef.content)) {
      for (const [key, value] of Object.entries(prismic)) {
        let paramRegex = new RegExp(`:PrismicReplace|${key}`);
        if (paramRegex.test(tagDef.content)) {
          tagDef.content = tagDef.content.replace(paramRegex, prismic[key]);
        }
      }
    }
javascript regex string replace
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.