【发布时间】:2020-10-18 12:10:05
【问题描述】:
我正在尝试检查字符串是否包含此字符 `。
如果找到该字符,则将该字符串包装在 <div> 之间。
例如:
`this is it
应该变成:
<div>this is it</div>
我已经尝试过,但似乎不起作用:
let arr = "`this is it";
if (arr.includes('`')) {
arr = arr.replace(/\`(\w+)\`/g, '<div>$1</div>');
}
有什么想法吗?
【问题讨论】:
-
试试
arr.replace(/`([^`]+)`?/g, '<div>$1</div>') -
不相关但不能重新分配
const变量 -
\w等价于[a-zA-Z0-9_],因此与空格不匹配。
标签: javascript html regex string