我有以下字符串。
This is content
[divider space="123" anything="123"]
more content
[divider space="123"]
some more text or shortcodes
挑战是首先找到字符串[divider ad然后同时查找]一旦发现添加[/ divider]结尾。所以最终的输出将成为:
This is content
[divider space="123" anything="123"][/divider]
more content
[divider space="123"][/divider]
some more text or shortcodes
使用以下代码我可以找到字符串。
str = 'This is content[divider space="123" anything="123"][/divider]more content[divider space="123"][/divider] some more text or shortcodes';
str = str.match(divider(.*)]");
我怎样才能找到第一个字符串然后是秒串并在此之后添加一些东西[/ divider]。
答案 0 :(得分:0)
您可以使用replace
功能并传递
$("p").html($("p").html().replace(/\[divider ([a-z]+\="\d+"\s?)+\]/g, s => s + "[/divider]" ));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
This is content
[divider space="123" anything="123"]
more content
[divider space="123"]
some more text or shortcodes
</p>