Greasy Fork is available in English.

게시판 » 개발

How to match string is not match

§
작성: 2017-02-22
수정: 2017-02-22

How to match string is not match

json = one two RANDOM three four five RANDOM six seven eight RANDOM nine zero

if found any word other than "one two three four five six seven eight nine zero" How i can match RANDOM string ?


if(json.match('one|two|three|four|five|six|seven|eight|nine|zero|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Zero')){

}else{

}

woxxom관리자
§
작성: 2017-02-22
수정: 2017-02-22

Assuming the parts are separated with a space, and you want to extract the RANDOM parts only:

var randoms = json
    .replace(/\b(one|two|three|four|five|six|seven|eight|nine)\b\s*/gi, '')
    .match(/\S+/g);

Since JS doesn't have negative look-ahead, we strip the recognized words first (globally i.e. all occurrences, case-insensitively), then all that's left is the random words which we extract using the non-whitespace token \S+.

§
작성: 2017-02-22

I want to remove all RANDOM

§
작성: 2017-02-22

But RANDOM is unknown word it's random word

§
작성: 2017-02-22
수정: 2017-02-22

json = one two RANDOM four
If (json.match(one|two|three|four) --> false

json = one two three four
If (json.match(one|two|three|four) --> true

Working like this http://www.regextester.com/15 but this RegEx not work with "one two RANDOM four"

woxxom관리자
§
작성: 2017-02-22

Ah! Then you need to strip all known words and see if the result is empty because JS regexp doesn't have negative look-ahead:

var hasRandom = json
    .replace(/\s*(one|two|three|four|five|six|seven|eight|nine)\s*/gi, '').trim();
if (!hasRandom) {

}
§
작성: 2017-02-22
수정: 2017-02-22

Okay now it's work thanks alot

댓글 남기기

댓글을 남기려면 로그인하세요.