試圖檢查以just.
(包括圓點)開頭的randomString
。使用正則表達式檢查以substring開頭的字符串
這應該給我假,但它並非如此:
var randomString = 'justanother.string';
\t
var a = randomString.match('^just\.');
\t
console.log(a);
我可能錯過了在正則表達式參數的東西。
試圖檢查以just.
(包括圓點)開頭的randomString
。使用正則表達式檢查以substring開頭的字符串
這應該給我假,但它並非如此:
var randomString = 'justanother.string';
\t
var a = randomString.match('^just\.');
\t
console.log(a);
我可能錯過了在正則表達式參數的東西。
您需要使用創建一個Regular Expression和使用.test()
方法。
var randomString = 'justanother.string';
var a = /^just\./.test(randomString)
console.log(a);
答案很簡單,你沒有propertly創建正則表達式。
'this is not regex'
/this is regex/
new RexExp('this is also regex')
var randomString = 'justanother.string';
\t
var a = randomString.match(/^just\./);
console.log(a);
// I sugest dooing something like this
const startsWithJust = (string) => /^just\./.test(string)
var randomString = 'justanother.string';
var another = 'just.....................';
console.log( randomString.match('^(just[.]).*'));
console.log( another.match('^just[.].*'));
如果你想保持你的線需要相同的只有一個變化。
var a = randomString.match('^just\\.');
您需要轉義第一個反斜槓。
noo這會匹配'just.',而且'just'和'只是............................... .. ........................' –