0

I am using regular expressions to create a simple JavaScript application. I've looked at some answers here from the community, and unfortunately I'm still unable to create a regular expression that suits my specific case. I apologize, but I have no mastery over regular expressions.

I wish I could solve my problem without using a reverse function, or any utility function if possible. If this can only be solved with one, then that's fine, there's nothing to be done.

THE PROBLEM

Consider the following text (it's in portuguese, Brazil, so accented characters need to be considered, like Álvares, Ligações, América, etc.):

O Brasil é um país com muitos brasileiros. Alguns dizem Abrasil, outros nem tanto.

What I seek:

"Brasil" true // Desired result
" Brasil" false
"brasil"eiros false
A"brasil" false

The regular expression currently used (not correct):

([^\w|^\u0080-\u00FF])(Brasil)(?![\w|\u0080-\u00FF])

The part of the expression I'm having trouble with:

 ([^\w|^\u0080-\u00FF])

The (wrong) result I am getting at the moment:

" Brasil" // I just want "Brasil"

Bibliography

Even accepted, from what I could see, look behind is not yet widely implemented.

Loa
  • 2,117
  • 3
  • 21
  • 45
  • What exactly do you need ? filter the sentence ? Sum up the number of occurrences in the sentence ? – Eduardo Lima Nov 30 '19 at 20:23
  • @user2541537 I want to be able to search for a term that is not within words. It can be written at the beginning, middle, and end of a sentence. I want to find only Brasil, without finding other words that have Brasil. Also, I don't want to match " Brasil" or "Brasil " or " Brasil ". Why? Because the found word will be replaced later, and I don't want to cut the white space, but just the word. – Loa Nov 30 '19 at 20:34
  • Don't worry about the leading character, just replace it (group 1) back with whatever is being replaced by Brazil. Example: replace `([^\w\u0080-\u00FF]|^)(Brasil)(?![\w\u0080-\u00FF])` with `$1Spain` See https://regex101.com/r/qU13FJ/1 –  Nov 30 '19 at 23:31

0 Answers0