Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

javascript - How to check a string includes a value or not in NodeJS?

We have a string comparison like below.

value1 = "?????? ??? {er}"
value2 = "?????????? errr"

enter image description here I have to check value 2 is same like value1 except for the err part.

  var separators = ['{er}','{Er}','{ER}','{#eR#}'];
    let dMsg = value1.split(new RegExp(separators.join('|'),'g'));
    let msg = true;
    for (var m = 0; m < dMsg.length; m++) {
        if (!value1.includes(dMsg[m])) {
            Status = "Fail"
            break;
        }
    }

Even though both strings are same, Im getting status as failure. Can anybody help me where I'm going wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, your code is not giving me Fail message.

dMsg will be an array as split function return an array of strings.

In your case, dMsg=['?????? ??? ','']

you should do dMsg=dMsg[0] before the loop starts. This should resolve your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...