2017-09-22 19 views
0

如何確保我的字符串中有使用打字稿的兩個單詞。如何確保我的字符串在打字稿中有兩個單詞

我需要這個的原因,以便我只會在名稱是「倒數第一」名稱格式時纔會調用服務器。

+1

'「倒數第一」 .split(」「)。長度=== 2' – Keith

+0

謝謝基思。 – Ashish

+1

如果你的字符串中有換行符或其他空格,這可能不會給出預期的結果 – JKillian

回答

4

答案真的不是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"));

+0

我會想'^ [A-Z] + [A-Z] + $',否則這會使'first \ nname''第一個\ tname'等也有效。 – Keith

+0

@Keith好的。答案已更新。 –

+0

'/^[A-Z] + [A-Z] + $ /。test(「[\\]^_ aBc AbC [\\]^_」)'返回true。當你指'[A-Za-z]'時,不要使用'[A-Z]'。或者考慮使用不區分大小寫的標誌'i'。 – kamoroso94

0

像這樣的事情不能打字稿鍵入,但你可以利用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) 
    } 
} 
0

您可以確保您的字符串中有兩個單詞,可能包含字母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+$/; 
相關問題