user1077220
user1077220

Reputation: 1523

Regular expressión that matches

I have this text:

Habitación 101

As you can see there is a letter with an accent: ó

Im looking for a regular expressión that matches also when the word is written without accent, that is "habitacion".

I'm using JavaScript's new RegExp(keyword, 'u').

Upvotes: 0

Views: 70

Answers (1)

cнŝdk
cнŝdk

Reputation: 32145

Well simply use Habitaci[ó|o]n as regex.

const regex = new RegExp('Habitaci[ó|o]n', 'gi');

Where [ó|o] matches a single character in the list ó|o.

This is a Demo:

const regex = new RegExp('Habitaci[ó|o]n', 'gi');
const str = `Habitación
Habitacion`;
let m;

while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  // The result can be accessed through the `m`-variable.
  m.forEach((match, groupIndex) => {
    console.log(`Found match, group ${groupIndex}: ${match}`);
  });
}

Upvotes: 1

Related Questions