简体   繁体   English

正则表达式在文本编辑器中仅匹配一次?

[英]Regular Expression match only one time in text editor?

Using text editor summernote , I tried to match every character starting with {{ , optionally white space , any character , optionally white space , and end with }} . 使用文本编辑器summernote ,我尝试匹配以{{开头{{可选为white spaceany character (可选为white space )和}}结尾的每个字符。

Therefore, I derived my regular expression pattern like this: 因此,我得出了这样的正则表达式模式:

var regex1 = /^\\{\\{\\s*?\\w+\\s*?\\}\\}$/g;

This would match: {{matched}} or {{ matched }} 这将匹配: {{matched}}{{ matched }}

 $('.wysiwyg').summernote({ callbacks: { onChange: function(contents, $editable) { // remove html tag contents = contents.replace(/<\\/?[^>]+(>|$)/g, ""); console.log('contens: ' + contents); var regex = /^\\{\\{\\s*?\\w+\\s*?\\}\\}$/g; // update to local storage if (regex.test(contents)) { console.log('regex1 matched'); } } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script> <div class="container"> <div class="row"> <form class="col-lg-12" id="postForm" action="/summernote.php" method="POST" enctype="multipart/form-data"> <fieldset> <legend>Summernote test</legend> <p class="container"> <textarea class="input-block-level wysiwyg" id="summernote" name="content" rows="18"> </textarea> </p> </fieldset> </form> </div> </div> 

However, it only matched if the starting of word is something like {{matched}} , and as contents is growing another subsequent matched after the first match like {{another_matched}} is not matched anymore. 但是,仅当单词的开头是{{matched}}类的单词时才匹配,并且随着内容的增长,在第一个匹配项{{another_matched}}不再匹配另一个后续匹配项。 Simply say it matched only once time only if content start with matched pattern, eg. 简单地说,仅当内容以匹配的模式开头时,它才匹配一次。 {{matched}} . {{matched}}

How can I make it to match a numbers of time the word, {{matched}} and {{another_matched}} , is matched ? 如何匹配单词{{matched}}{{another_matched}}匹配的时间? Thanks. 谢谢。

Currently, your regex only matches string that completely match the pattern ie whole string has to match. 当前,您的正则表达式仅匹配与模式完全匹配的字符串,即整个字符串必须匹配。

This is because you added ^ and $ at the start and end of the regex. 这是因为您在正则表达式的开头和结尾添加了^$ These are anchors for the start and end of the string. 这些是字符串开头和结尾的锚点。 It asserts that the string should start with your required pattern and also end with the required pattern, so something like this won't match: 它断言该字符串应以所需的模式开头,也应以所需的模式结尾,因此类似这样的内容将不匹配:

Hello {{match}} Bye

Just remove the ^ and $ and it will search the whole string for the pattern. 只需删除^$ ,它将在整个字符串中搜索该模式。

regex = /\{\{\s*?\w+\s*?\}\}/g;
regex.test('{{matched}} something {{matched}}') // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM