简体   繁体   中英

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 }} .

Therefore, I derived my regular expression pattern like this:

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

This would match: {{matched}} or {{ 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. Simply say it matched only once time only if content start with matched pattern, eg. {{matched}} .

How can I make it to match a numbers of time the word, {{matched}} and {{another_matched}} , is 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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