我有一个字符串,需要用空格分割它,但如果括号内有一些单词,我需要跳过它。
例如,
input: 'tree car[tesla BMW] cat color[yellow blue] dog'
output: ['tree', 'car[tesla BMW]', 'cat', 'color[yellow blue]', 'dog']
Run Code Online (Sandbox Code Playgroud)
如果我使用 simple ,.split(' ')它会进入括号内并返回不正确的结果。
另外,我试图写一个正则表达式,但没有成功:(
我的最后一个正则表达式看起来像这样.split(/(?:(?<=\[).+?(?=\])| )+/)并返回["tree", "car[", "]", "cat", "color[", "]", "dog"]
非常感谢您的帮助
在我的组件内部,ngOnInit我有一个subscribe()内部调用组件函数:
ngOnInit(): void {
this.navigationService.itemReset$.subscribe(event => this.resetItem(event));
}
Run Code Online (Sandbox Code Playgroud)
在导航服务中我有:
btnPressed$: Subject<Event> = new Subject();
itemReset$: Observable<Event> = this.btnPressed$.asObservable();
keyDown(event: Event): void {
this.btnPressed$.next(event);
}
Run Code Online (Sandbox Code Playgroud)
我搜索了很多关于这个问题的信息,并在我的组件测试中尝试了类似的方法:
ngOnInit(): void {
this.navigationService.itemReset$.subscribe(event => this.resetItem(event));
}
Run Code Online (Sandbox Code Playgroud)
但仍然出现错误
TypeError: this.navigationService.itemReset$.subscribe is not a function
Run Code Online (Sandbox Code Playgroud)
知道吗,为什么它不起作用?我将非常感谢任何帮助!