1

I have the following input text:

Teterboro US [TEB] - 20KM

I would like to get the following output :

Teterboro US

I am using the following expression :

.*\[

But it I am getting the following result

Teterboro US [

I would like to get rid of the last space and bracket " ["

I am using JavaScript.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex Lavriv
  • 321
  • 1
  • 10

4 Answers4

1

You can use /.*?(?=\s*\[)/ with match; i.e. change \[ to look ahead (?=\s*\[) which asserts the following pattern but won't consume it:

var s = "Teterboro US [TEB] - 20KM";

console.log(
  s.match(/.*?(?=\s*\[)/)
)
Psidom
  • 209,562
  • 33
  • 339
  • 356
1

You can try this pattern:

.*(?= \[)

It is positive lookahead assertion and it works just like you expect.

cn007b
  • 16,596
  • 7
  • 59
  • 74
0

Another option (worth mentioning in case you're not familiar with groups) is to catch only the relevant part:

var s = "Teterboro US [TEB] - 20KM";
console.log(
  s.match(/(.*)\[/)[1]
)

The regex is:

(.*)\[

matches anything followed by "[", but it "remembers" the part surrounded by parenthesis. Later you can access the match depending on the tool/language you're using. In JavaScript you simply access index 1.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

You could use: \w+\s+\w+? Depending upon your input

HoleInVoid
  • 39
  • 6