使用JavaScript,如果另一個字符串以第一個字符串開頭,如何從其他字符串剝離字符串?從其他字符串剝離字符串(如果它使用JavaScript開頭)
var matcher='Hello';
var s1='hello everyone';
console.log(someFunction(s1,matcher);
//Should return ' everyone', and should be case insensitive
var s2='xhello everyone';
console.log(someFunction(s2,matcher);
//Should return nothing (either NULL or FALSE or "" would be fine) since s2 doesn't start with 'Hello'
演示了三個答案:https://jsfiddle.net/dynvgn1k/
搜索第一個字符串,並用'nothing'替換( 「」)。 –
@noob如何強制它從一開始就開始? – user1032531
使用'^'。這從開始就被稱爲錨定。它會在字符串的開頭尋找你的匹配。 –