-1

I found the regExp

            var pattern:RegExp = new RegExp("\(\?\<\=\\s)" + string + "(?=[\\s|\,])", "ig");  

someone wrote it back in 2011.

I want to use it in my code in order to find the word string in a given text. It has to be a whole word. The problem is that it finds the "string" only if it is not at the begining of the text and if it is not at the end of the text. what can I do in order to find also the first and the last line of the text (the first word doesn't have a space before it, and the last one doesnt have a space or a punctuation after it.

Eli Cohen
  • 131
  • 1
  • 10
  • I omitted the last part, and it works. I just want to understand what is in the last part, why it is needed? what's ?= – Eli Cohen Feb 09 '18 at 00:06
  • 1
    Possible duplicate of [Regex match entire words only](https://stackoverflow.com/questions/1751301/regex-match-entire-words-only) – bobble bubble Feb 09 '18 at 00:23
  • What language is this in? `?=` is a zero-width assertion that there will be a match at that point. I don't think whoever wrote the pattern understood regex too well the character class escaping is funny. – NetMage Feb 09 '18 at 00:25
  • It seems \b is not working for me in actionscript. – Eli Cohen Feb 09 '18 at 00:41
  • If lookbehinds were allowed in ECMAScript, I'd suggest this `(?<!\S)string(?![^\s,])` Which is known as whitespace boundary behind and ahead (plus a comma boundary) –  Feb 09 '18 at 01:19
  • I don't see mentioning someone wrote the regex in 2011 has anything to do to help people understand your problem. – Adrian Shum Feb 09 '18 at 07:58
  • I have to give you a big -1 for not knowing anything about regular expressions and thereby picking the wrong answer. If you need to know why the answer is junk, just let me know... –  Feb 09 '18 at 16:32

2 Answers2

2

To match a whole word, you need to match a word boundary at either end. ActionScript regular expressions match at the start and end of the string as a boundary as well.

var pattern:RegExp = new RegExp("\\b" + string + "\\b", "ig");

Note if string could contain regular expression meta characters, you should be quoting them.

NetMage
  • 26,163
  • 3
  • 34
  • 55
  • works great on the first word but not on the last. I tried the \b with one slash now I now it has to be with 2 slashes. – Eli Cohen Feb 09 '18 at 11:00
  • I am very sorry, It works great also with the last word! (the reason it didnt work before wan not because of the regExp but because of another programatic problem I had in my code. Thanks! – Eli Cohen Feb 09 '18 at 11:56
0

?= is a positive lookahead. It say that your text must be followed by the content of the parenthesis (so the searched text cannot be the last word).

To make your replace, you could use the following regexp:

(?<=(^|\s))text(?=([\s,]|$))

Remember to add the '\' when you'll add it to your RegExp string (take example from the RegExp you pasted in your question).

You can try it from the javascript console (it works only in Chrome, the other browser doesn't support the lookbehind):

"text ciao text texttext a, a".replace(/(?<=(^|\s))text(?=([\s,]|$))/ig,"x")

Return "x ciao x texttext x, x", as expected (texttext is not replaced because it isn't the searched word).

NetMage
  • 26,163
  • 3
  • 34
  • 55
debe
  • 1,752
  • 2
  • 12
  • 11
  • What can i do if i want it to be the last word too? same about the first word. I saw that in regex there is \b which is a word boundry, but it doesn't work in regExp. I am in Actionscript 3.0. – Eli Cohen Feb 09 '18 at 00:39
  • @EliCohen, I've added a regexp in my answer – debe Feb 09 '18 at 01:23
  • I get no replacement. I tries with one slash and with 2 slashes befor the s, and it replaces nothing. – Eli Cohen Feb 09 '18 at 11:17