简体   繁体   English

如何测试字符串是否依次包含多个定义的单词?

[英]How to test whether string contains multiple defined words in order?

I have a string and need to test whether multiple defined words are included in order. 我有一个字符串,需要测试是否按顺序包含多个定义的单词。

For example the predefined words are: ["one", "two", "three"] 例如,预定义的单词是: ["one", "two", "three"]

So a string with: "one two three" should return true but a string with: "three two one" should return false. 因此,带有"one two three"的字符串应返回true,但带有"three two one"的字符串应返回false。

My current regex looks like this: new RegExp('\\\\bone\\\\b\\\\two\\\\b\\\\three\\\\b') 我当前的正则表达式如下: new RegExp('\\\\bone\\\\b\\\\two\\\\b\\\\three\\\\b')

Unfortunately this just checks if the words are present but doesn't care if they are in the right order. 不幸的是,这只是检查单词是否存在,而不关心它们的顺序是否正确。

You can just use this regex 您可以只使用此正则表达式

var arr = ["one", "two", "three"];
var regex = new RegExp(arr.join(".*"));

arr.join(".*") will make a regex /one.*two.*three/ arr.join(".*")将使正则表达式为/one.*two.*three/

You can use the following regular expression. 您可以使用以下正则表达式。

var re = /one.*two.*three/

Example: 例:

re.test('one two three'); // true
re.test('three two one'); // false

Sir please check these what's the error. 先生,请检查这些是什么错误。 I can't figure this out. 我不知道这个。 The code don't pop up. 代码不会弹出。

Here is the pop up click. 这是弹出的点击。

<li><a href="#"><i class="icon-envelope icon-2x" onClick="click();"></i></a></li>

Here is the javascript codes. 这是JavaScript代码。

<script type="text/javascript" src="//www.klaviyo.com/media/js/public/klaviyo_subscribe.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){
    $('icon-envelope icon-2x').on('click',function(){
     KlaviyoSubscribe.attachModalSignUp({
        list: 'g9cZtX',
        delay_seconds: 0.5,
        content: {
            clazz: ' klaviyo_modal_g9cZtX',
            header: "Interested in our Newsletter?",
            subheader: "Stay in the know with news and promotions.",
            button: "Subscribe",
            success: "Thanks! Check your email for a confirmation.",
            styles: '.klaviyo_modal.klaviyo_modal_g9cZtX {  font-family: "Helvetica Neue", Arial}.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_header {  color:#222;}.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_subheader {  color:#222;}.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_submit_button,.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_submit_button span {  background-color:#07234F;  background-image: none;   border-radius: 2px;}.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_submit_button:hover,.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_submit_button span:hover {  background-color:#2A4D85;  background-image: none; }.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_inner,.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_fieldset .klaviyo_field_group input[type=text],.klaviyo_modal.klaviyo_modal_g9cZtX .klaviyo_fieldset .klaviyo_field_group input[type=email] {  border-radius: 2px;}'
        }
    });
});
});
    </script>

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

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