I want to match a combination of and/or expression and match only sentences in between for example
test1 test2 and test2 or test4 test5
I want to get matches test1 test2, test2, test4 test5
I tried this regex https://regex101.com/r/FFLtg6/1 but it doesn't work :
(. )(((and|or)\s )?)
Update : I need a regex expression because it is part of a bigger regex.
CodePudding user response:
Here's what you're looking for:
const txt = "lorem ipsum and dolor and sit or amet consecteturand elit";
console.log(txt.split(/ and | or /));
CodePudding user response:
I would probably just do the obvious:
const rxWords = /\S /g;
const rxConnectors = /^(and|or)$/i;
const nonConnector = w => ! rxConnectors.test(w) ;
function getInterestingWords( s ) {
const corpus = s ?? '' ;
const matches = corpus.match( rxWords ) ;
const words = matches.filter( nonConnector ) ;
return words;
}
Given that, executing
const text = 'test1 test2 and test2 or test4 test5f' ;
const interestingWords = getInterestingWords(text);
sets interestingWords
to the expected
[ 'test1', 'test2', 'test2', 'test4', 'test5' ]