var str = "I have a <animal>, a <flower>, and a <car>.";
私はタイガー、ローズやBMWと以下
<animal> , <flower> and <car>
を置換する上記の文字列を、このための最善のアプローチを提案してください。
私はTypescriptでAngular 2 Applicationに取り組んでいます。
var str = "I have a <animal>, a <flower>, and a <car>.";
私はタイガー、ローズやBMWと以下
<animal> , <flower> and <car>
を置換する上記の文字列を、このための最善のアプローチを提案してください。
私はTypescriptでAngular 2 Applicationに取り組んでいます。
代替関数を含む正規表現を使用して、各一致を評価し、適切な置換を返すことができます。あなたは活字体を使用している場合
var str = "I have a <animal>, a <flower>, and a <car>.";
function replace(source : string, replacements : { [name:string]: string }) {
return str.replace( new RegExp("<([A-z]*)>", "g"), (m) => {
return replacements[m.substring(1, m.length -1)];
});
}
console.log(replace(str, {
"animal" : "Tiger",
"flower" : "Rose",
"car" : "BMW"
}))
以下のようにすると、関数本体は次のようになります。 'return str.replace(/ <([A-z]*)>/g、m => replacement [m.substring(1、m.length - 1) ]); ' – Duncan
@Duncanあなたはそのように短くなりましたが、長さの長い表現であったので、複数行ではよりよく見えました –
あなたは角度スタイルガイド(連続配信パイプラインでNG糸くずで例えば施行)の勧告である単一引用符let str = 'I have a <animal>, a <flower>, and a <car>.';
注意とhttps://www.typescriptlang.org/docs/handbook/basic-types.htmlに応じて、あなたの文字列を宣言する必要があります。代わりに、あなたはコードですでにプレースホルダを置き換えることができます方法を置き換える共通JavaScriptを使用して交換するの
:
の
可能な重複[JavaScriptで文字列の出現をすべて置き換えるには?](HTTPS: //stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) –
@JoeClay通常の文字列はOKですが、またはまたはの問題に直面しています文字列。 –
AmiDeveloper
http://idownvotedbecau.se/noattempt – Sefe