英文:
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 /(?<=\[\[).*?(?=\]\])/g
will do the trick.
Explanation:
(?<=\[\[)
- 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(/(?<=\[\[).*?(?=\]\])/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(/(?<=\[\[).*?(?=\]\])/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 => map[val.slice(2, -2)])
}
console.log(replace(input, { recipient: "Bob", username: "b00", password: "12345" }))
<!-- 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) => (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);
<!-- end snippet -->
Regex explaination:
\[\[
- matches string "[["
(\w+)
- matches any word characters
\]\]
- matches string "]]"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论