从字符串中提取位于“[[”和“]]”之间的值。

huangapple go评论59阅读模式
英文:

Grab value in between "[[" and "]]" from string

问题

我正在开发一个网络应用程序,用户可以创建文本模板。例如,当用户创建新模板时,他们会像这样写入任何可变文本:[[variable]]

示例:

嗨[[recipient]],

这是您的登录凭据:

用户名:[[username]]

密码:[password]

当用户提交模板表单时,我需要搜索内容字符串并将所有创建的变量提取并放入一个数组中。所以对于这个示例,我尝试获取一个看起来像这样的数组:

[recipient, username, password]
英文:

I'm working on a web app where users can create text templates. For example when the user is creating a new template they would write any variable text like so [[variable]].

Example:

Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]

When the user submits the template form I need to search through the content string and get all the variables that they created and put them in an array. So for this example I'm trying to get an array that looks like this:

[recipient,username,password]

答案1

得分: 1

Following regexp /(?<=\[\[).*?(?=\]\])/g will do the trick.
Explanation:

  • (?<=\[\[) - 正向回顾后查找以[[开头的内容,不包括[[本身。
  • .*? - 匹配两者之间的任何内容(包括空字符串)。
  • (?=\]\]) - 正向预查以]]结尾的内容,不包括]]本身。

Here is an example:

const text = `Hi [[recipient]],
Here are your login credentials:
Username: [[username]]
Password: [password]`

const matchedParams = text.match(/(?<=\[\[).*?(?=\]\])/g)
console.log(matchedParams)
英文:

Following regexp /(?&lt;=\[\[).*?(?=\]\])/g will do the trick.
Explanation:

  • (?&lt;=\[\[) - positive lookbehind to match anything thats starts with [[, not including it.
  • .*? - match anything between (including empty string).
  • (?=\]\]) - positive lookahead to match anything thats ends with ]], not including it.

Here is an example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const text = `Hi [[recipient]],
Here are your login credentials:
Username: [[username]]
Password: [password]`

const matchedParams = text.match(/(?&lt;=\[\[).*?(?=\]\])/g)
console.log(matchedParams)

<!-- end snippet -->

答案2

得分: 0

我们可以简单地使用正向和反向预查:

const input = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]`

const matches = [...(input.match(/(?<=\[\[).*?(?=\]\])/g) || [])]
console.log(matches)

在JavaScript中,如果需要,也可以使用内置的replace函数来替换这些值:

const input = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]`

function replace(input, map) {
  return input.replace(/\[\[.*?\]\]/g, val => map[val.slice(2, -2)])
}

console.log(replace(input, { recipient: "Bob", username: "b00", password: "12345" }))

请注意,这些代码示例是用英文编写的,我已提供中文翻译,但代码本身没有进行翻译。

英文:

We can simply use lookarounds:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]`

const matches = [...(input.match(/(?&lt;=\[\[).*?(?=\]\])/g) || [])]
console.log(matches)

<!-- end snippet -->

In JavaScript it's also simple to replace the values if you need to with the built-in replace function:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const input = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]`

function replace(input, map) {
  return input.replace(/\[\[.*?\]\]/g, val =&gt; map[val.slice(2, -2)])
}

console.log(replace(input, { recipient: &quot;Bob&quot;, username: &quot;b00&quot;, password: &quot;12345&quot; }))

<!-- end snippet -->

答案3

得分: 0

You can use a simple regex and map with slice remove [[ and ]] from results


const grabVals = (str) => (str.match(/\[\[(\w+)\]\]/g) || []).map(el => el.slice(2, -2));
const str = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]`;
const vals = grabVals(str);

console.log(vals);

英文:

You can use a simple regex and map with slice remove [[ and ]] from results

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const grabVals = (str) =&gt; (str.match(/\[\[(\w+)\]\]/g) || []).map(el =&gt; el.slice(2, -2));
const str = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [password]`;
const vals = grabVals(str);

console.log(vals);

<!-- end snippet -->

Regex explaination:

\[\[ - matches string "[["

(\w+) - matches any word characters

\]\] - matches string "]]"

huangapple
  • 本文由 发表于 2023年4月20日 05:27:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定