回答
答案真的不是TypeScript的依賴。這是基本的JavaScript。
您可以在字符串中使用正則表達式來perform a test:
function testString(input){
// Return whether or not there are letters (any amount and any case)
// followed by one space and then more letters (any amount and any case).
// Of course, if you wanted to get more specific about case or character
// counts, adjusting the regular expression would be simple.
return /^[A-Za-z]+ [A-Za-z]+$/.test(input);
}
console.log(testString("Scott"));
console.log(testString("Scott Marcus"));
console.log(testString("Scott\nMarcus"));
我會想'^ [A-Z] + [A-Z] + $',否則這會使'first \ nname''第一個\ tname'等也有效。 – Keith
@Keith好的。答案已更新。 –
'/^[A-Z] + [A-Z] + $ /。test(「[\\]^_ aBc AbC [\\]^_」)'返回true。當你指'[A-Za-z]'時,不要使用'[A-Z]'。或者考慮使用不區分大小寫的標誌'i'。 – kamoroso94
像這樣的事情不能打字稿鍵入,但你可以利用type guards到明確指出param值需要首先通過特定測試才能成爲函數的有效輸入。
這裏是斯科特的回答是 「強類型」 的版本可能是什麼:
我定製-types.d.ts
// A string supertype that represents a string that passed our RegEx test
declare type TwoWords = Branded<string, 'two-words'>
// Used to declare an unique primitive type, "nominal types" aren't supported yet
// @see https://github.com/Microsoft/TypeScript/issues/202
declare type Branded<T, U> = T & { '__brand': U }
// FullName in a "first last" name format
declare type FullName = TwoWords
我-test.ts
// This a type guard that casts strings with two words to TwoWords
function isTwoWords(input: string): input is TwoWords {
return /^[A-Za-z]+ [A-Za-z]+$/.test(input)
}
// This is the function that should only receive strings with two words
function showName(name: FullName) {
let [first, last] = name.split(' ') // Can be splited with safety
console.log(`${last}, ${first}`)
}
let name1 = 'John'
let name2 = 'John Doe'
let name3 = 'John Doe Junior'
// Error, you can't assume this string has two words before testing it
showName(name2)
for (let name of [name1, name2, name3]) {
if (isTwoWords(name)) {
// No errors, TS knows that only a TwoWords type reach this line
showName(name)
}
}
您可以確保您的字符串中有兩個單詞,可能包含字母ccents,多個連字符,多個撇號之內,以及由RegEx分隔的空格。
const allowedChars = "a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF"; // https://stackoverflow.com/a/1073545/188246
const isTwoWordNameRegEx = new RegExp(`^[${allowedChars}]+(['\-][${allowedChars}]+)* [${allowedChars}]+(['\-][${allowedChars}]+)*$`, "i");
isTwoWordNameRegEx.test("Sébastien Doe"); // true
isTwoWordNameRegEx.test("John Doe"); // true
isTwoWordNameRegEx.test("John Doe-Williams") // true
isTwoWordNameRegEx.test("Scarlett O'Hara") // true
isTwoWordNameRegEx.test("John Doe-Williams-Jane") // true
isTwoWordNameRegEx.test("John Doe-") // false
isTwoWordNameRegEx.test("John Doe'") // false
isTwoWordNameRegEx.test("John' Doe") // false
isTwoWordNameRegEx.test("John Doe Williams") // false
現在,我已經提到了這一點...... 不這樣做!它仍然在假設一個名字如何。請閱讀Falsehoods Programmers Believe About Names。
如果你真的想將其限制於兩個詞,那麼請考慮一個非常寬鬆的版:
const isTwoWordNameRegEx = /^\S+ \S+$/;
- 1. php - 如何在字符串中的兩個單詞之間插入單詞?
- 2. 打字稿中的字符串類型
- 3. 如何保存字符串,在Python中每列一個單詞?
- 4. 在java中比較兩個字符串,如何打印等於單詞
- 5. 如何確定字符串中的單詞是否是雙字?
- 6. 如何反轉字符串中兩個單詞的順序?
- 7. 如何迭代打字稿中的字符串文字類型
- 8. 從我的字符串中提取最後兩個單詞
- 9. 「[指數:字符串]」:在打字稿
- 10. 字符串轉換爲在打字稿
- 11. 如何在打字稿中進行字符串插值?
- 12. 我該如何大寫字符串中所有單詞的第一個字母?
- 13. 如何確保在一個字符串中只有一個http://?
- 14. 在多字詞字符串中匹配單詞的單詞
- 15. 將字符串中的單詞連接成單個字符串
- 16. 將兩個單詞分成不同的單詞串[但有時只有字符串中的單詞...]
- 17. 如何在兩個單獨的字符串中查找匹配的單詞?
- 18. 如何推送打字稿文件中的字符串?
- 19. 如何確保打字稿模塊符合接口
- 20. 如何修整字符串右側的兩個單詞
- 21. 匹配兩個字符串中的單詞時的字符串匹配算法?
- 22. 將兩個單詞中的第一個字母大寫爲兩個字符串
- 23. 如何在字符串中打印迴文單詞?
- 24. 打字稿參數:在任何一個字符串參數
- 25. 字符串到單詞的字符串[]
- 26. 在c中的兩個單詞之間提取字符串#
- 27. 在KornShell中選擇兩個單詞之間的字符串
- 28. 在Python中打印字符串中的每個單詞的第一個字母(保留標點符號)
- 29. 在數組中打印字符串打印兩個字符?
- 30. 打字稿 - 字符串字面型,將字符串
'「倒數第一」 .split(」「)。長度=== 2' – Keith
謝謝基思。 – Ashish
如果你的字符串中有換行符或其他空格,這可能不會給出預期的結果 – JKillian