Kaepxer
Kaepxer

Reputation: 380

Multiple OR conditions for words in JavaScript regular expression

I trying to have a regular expression which is finding between two words but those words are not certain one.

2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303

This is my text. I'm trying to find the word between Soyadı and Sınıfı, in this case ERTANĞA, but the word Sınıfı also can be no, numara or any number. This is what I did.

soyad[ıi](.*)S[ıi]n[ıi]f[ıi]|no|numara|[0-9]

[ıi] is for Turkish character issue, don't mind that.

Upvotes: 1

Views: 387

Answers (3)

The fourth bird
The fourth bird

Reputation: 163277

You can use a single capture group to get the word ERTANĞA, keep the character class [ıi] instead of using an alternation for (ı|i) and group the alternatives at the end of the pattern using a non capture group (?:

soyad[ıi](.+?)(?:S[ıi]n[ıi]f[ıi]|n(?:o|umara)|[0-9])
  • soyad[ıi] Match soyadı or soyadi
  • (.+?) Capture group 1, match 1 or more chars as least as possible
  • (?: Non capture group
    • S[ıi]n[ıi]f[ıi] Match S and then ı or i etc..
    • | Or
    • n(?:o|umara) Match either no or numara
    • | Or
    • [0-9] Match a digit 0-9
  • ) Close non capture group

Note that you don't need the /m flag as there are no anchors in the pattern.

Regex demo

const regex = /soyad[ıi](.+?)(?:S[ıi]n[ıi]f[ıi]|n(?:o|umara)|[0-9])/gi;
const str = "2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303\n";
console.log(Array.from(str.matchAll(regex), m => m[1]));

Upvotes: 1

Rukshán Dikovita
Rukshán Dikovita

Reputation: 574

You can use something like below :

/.*Soyad(ı|i)|S(ı|i)n(ı|i)f(ı|i).*|no.*|numera.*|[0-9]/gmi

Here is the link I worked on : https://regex101.com/r/QXLjLF/1

enter image description here

In JS code:

const regex = /.*Soyad(ı|i)|S(ı|i)n(ı|i)f(ı|i).*|no.*|numera.*|[0-9]/gmi;

var str = `2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303`;


var newStr = str.replace(regex, '');
console.log(newStr);

Upvotes: 2

mplungjan
mplungjan

Reputation: 177841

This might do it

const str = `2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnumaraE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnoE10/ENo303`
const re = /(?:Soyad(ı|i))(.*?)(?:S(ı|i)n(ı|i)f(ı|i)|no|numara)/gmi
console.log([...str.matchAll(re)].map(x => x[2]))

ES5

const str = `2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnumaraE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnoE10/ENo303`
const re = /(?:Soyad(ı|i))(.*?)(?:S(ı|i)n(ı|i)f(ı|i)|no|numara)/gmi
const res = []
let match;
while ((match = re.exec(str)) !== null) res.push(match[2])

console.log(res)

Upvotes: 1

Related Questions