In Actionscript, how to match / in infinitive stru

2019-07-20 03:49发布

问题:

I'm using the following regular expression to find the exact occurrences in infinitives. Flag is global.

(?!to )(?<!\w) (' + word_to_search + ') (?!\w)

To give example of what I'm trying to achieve

  • looking for out should not bring : to outlaw
  • looking for out could bring : to be out of line
  • looking for to should not bring : to etc. just because it matches the first to

I've already done these steps, however, to cross out/off should be in the result list too. Is there any way to create an exception without compromising what I have achieved?

Thank you.

回答1:

I'm still not sure I understand the question. You want to match something that looks like an infinitive verb phrase and contains the whole word word_to_search? Try this:

"\\bto\\s(?:\\w+[\\s/])*" + word_to_search + "\\b"

Remember, when you create a regex in the form of a string literal, you have to escape the backslashes. If you tried to use "\b" to specify a word boundary, it would have been interpreted as a backspace.



回答2:

I know OR operator but the question was rather how to organize the structure so it can look ahead and behind. I'm going to explain what I have done so far

var strPattern:String = '(?!to )(?<!\w) (' + word_to_search + ') (?!\w)|';
strPattern+='(?!to )(?<!\w) (' + word_to_search + '\/)|';
strPattern+='(?!to )(\/' + word_to_search + ')';
var pattern:RegExp = new RegExp(strPattern, "g");

First line is the same line in my question, it searches structures like to bail out for cases where you type out. Second line is for matching structures like to cross out/off. But we need something else to match to cross out/off if the word is off. So, the third line add that extra condition.