Обсуждения » Разработка

How to match string is not match

§
Создано: 22.02.2017
Отредактировано: 22.02.2017

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Мод
§
Создано: 22.02.2017
Отредактировано: 22.02.2017

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

I want to remove all RANDOM

But RANDOM is unknown word it's random word

§
Создано: 22.02.2017
Отредактировано: 22.02.2017

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"

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) {

}
§
Создано: 22.02.2017
Отредактировано: 22.02.2017

Okay now it's work thanks alot

Ответить

Войдите, чтобы ответить.