0

I have a string, which includes a title I want to find (-title or -t switch).

some_string -title "myTitle" some_more_string

For this I created a regular expression, which worked during development and on Android + Windows devices. After releasing the page users complained, that the page shows a blank page for iOS devices. After checking and googling it seems the mentioned title check is the cause. (Works in Chrome, but breaks in Safari: Invalid regular expression: invalid group specifier name /(?<=\/)([^#]+)(?=#*)/)

My title regular expression uses look-around assertions, which are reported bad for iOS.

import { Pipe, PipeTransform } from '@angular/core';

const titleExpression: RegExp = /(?<=\-t(itle)? ")[^"]+/;

@Pipe({
  name: 'findTitle'
})
export class FindTitlePipe implements PipeTransform {
  transform(value: string): string {
    return value ? value.match(titleExpression)[0] : '';
  }
}

However, I am a little bit lost on an alternative solution. Please advice.


Edit:

I now decided to use a split approach. (which also has yet to be improved, but that is another story)

transform(value: string): string {
    const tokens: string[] = value.split(' ');
    const foundAt: number = tokens.findIndex(token => ['-t', '-title'].includes(token));

    return foundAt === -1 ? '' : tokens[foundAt + 1].replace('"', '');
  }
MoxxiManagarm
  • 8,735
  • 3
  • 14
  • 43

1 Answers1

1

I suggest that you use a simple named capture:

const str = 'some_string -title "myTitle" some_more_string'
const titleExpression = /-t(itle)?\s*"(?<title>[^"]+)"/;
const title = str => str ? str.match( titleExpression ).groups.title : '';
console.log( title( str ) );
Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15