Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度-标题-我们可以用像';在';非资本化?_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 角度-标题-我们可以用像';在';非资本化?

Javascript 角度-标题-我们可以用像';在';非资本化?,javascript,angular,typescript,Javascript,Angular,Typescript,我见过TitleCase用于将字符串中的每个单词大写,这很有用,但是 我想输出存储为全小写的电影标题。我可以使用滴定酶,但这不会给出人们通常期望的外观 例如,电影标题(小写): 胆小鬼罗伯特·福特暗杀杰西·詹姆斯 我可以使用滴定酶进行输出: 胆小鬼罗伯特·福特暗杀杰西·詹姆斯 然而,当在杂志或电影海报上看到印刷品时,'of和'by的'O和B从不大写 import { Pipe, PipeTransform } from '@angular/core' @Pipe({ name: 'titleC

我见过TitleCase用于将字符串中的每个单词大写,这很有用,但是

我想输出存储为全小写的电影标题。我可以使用滴定酶,但这不会给出人们通常期望的外观

例如,电影标题(小写):

胆小鬼罗伯特·福特暗杀杰西·詹姆斯

我可以使用滴定酶进行输出:

胆小鬼罗伯特·福特暗杀杰西·詹姆斯

然而,当在杂志或电影海报上看到印刷品时,'of'by'OB从不大写

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

@Pipe({ name: 'titleCaseExcept' })
export class TitleCaseExceptPipe implements PipeTransform {
  transform(value: string): string {
    if (!value) return value; // safeguard

    // regex to find all words EXCEPT the ones we don't want capitalized
    let words: RegExp = /\b(?!of|by|the)\w+/g;
    // capitalize the first letters of said words
    let newVal = value.replace(words, (match) => {
      return match.replace(/^\w/, (word) => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase());
    })

    // always capitalize the first character of newVal
    return newVal.charAt(0).toUpperCase() + newVal.substr(1);
  }
}
除了简单地使用
{{movie.title | titlecase}}
之外,有没有一种简单的方法可以让我获得想要的外观


我知道我可以创建一个字典并在字符串中搜索关键字,但我想要一些可能更简单的东西…

如果您仅用于显示目的,建议使用默认管道,因为框架将处理大小写转换。

您可以编写自己的管道,将每个单词大写,但不希望大写的单词除外

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

@Pipe({ name: 'titleCaseExcept' })
export class TitleCaseExceptPipe implements PipeTransform {
  transform(value: string): string {
    if (!value) return value; // safeguard

    // regex to find all words EXCEPT the ones we don't want capitalized
    let words: RegExp = /\b(?!of|by|the)\w+/g;
    // capitalize the first letters of said words
    let newVal = value.replace(words, (match) => {
      return match.replace(/^\w/, (word) => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase());
    })

    // always capitalize the first character of newVal
    return newVal.charAt(0).toUpperCase() + newVal.substr(1);
  }
}

在正则表达式中添加您认为合适的单词。

但是如果我使用
|titlecase
我会得到所有大写单词,例如
Of
,当我需要小写
Of
时,在这种情况下,我也会建议由@oramWrong建议提供的解决方案。您可以编写自己的管道来包装titlecase管道,然后根据需要转换为小写。哦?你能告诉我一个消息来源吗?这样我就可以了解它了?