javascript 中String.match()與RegExp.exec()的差異說明_javascript技巧
1. 這兩個方法,如果匹配成功,回傳一個數組,則匹配失敗,回傳null。
2. 當RegExp的global屬性為false時,這兩個方法的回傳陣列是一樣的。
數組的第0個元素是整個pattern的第一個匹配字串,接下來的元素是pattern第一個匹配中的子匹配字串。
此外,陣列還有index和input兩個額外屬性,index是匹配字串的起始位置,input是整個輸入字串。
此時,RegExp的lastIndex屬性一直是0。
demo:
var s = 'this is string';
var p = /bw*(i)sb/;
var rm = s.match(p);
var re = p.exec(s);
console.log ('match_array: ' JSON.stringify(rm));
console.log('match_array_index: ' rm.index);
console.log('match_array_input: ' rm.input);
console. log('----------------------------');
console.log('exec_array: ' JSON.stringify(re ));
console.log('exec_array_index: ' re.index);
console.log('exec_array_input: ' re.input);
顯示控制結果為(firefoxfox台):
match_array: ["this","i "]
match_array_index: 0
match_array_input: this is a string
----------------------------
exec_array: ["this","i"]
exec_array_index: 0
exec_array_input: this is a string
3. 當RegExp的global為true是不同的。
match方法傳回的陣列包含所有符合字串,沒有子匹配字串和額外屬性。此時,lastIndex屬性無效。
exec方法傳回的陣列格式與global為false時一樣,只是此時RegExp的lastIndex屬性有效,匹配是從lastIndex所指示的字元開始的,並且方法執行後會將lastIndex置為本次匹配字串的下一個字元處,所以循環執行exec方法時會依序匹配整個字串,直到字串最後傳回null,並將lastIndex置0。
demo:
var s = 'this is string';
var p = /bw*(i)sb/g;
var rm = s.match(p);
var re;
console.log('match_array: ' JSON .stringify(rm));
console.log('match_array_index: ' rm.index);
console.log('match_array_input: ' rm.input);
while(re = p.exec( s)){
console.log('----------------------------');
console.log( 'exec_array: ' JSON.stringify(re));
console.log('exec_array_index: ' re.index);
console.log('exec_array_input: ' re.input);
console.log ('regexp_lastIndex: ' p.lastIndex);
}
console.log('---------------------------- ');
console.log('exec_array: ' re);
console.log('regexp_lastIndex: ' p.lastIndex);
結果:
match_array: ["this","is"]
match_arrayd 🎜>match_array_input: undefined
----------------------------
exec_array: ["this","i"]
exec_array_index: 0
exec_array_input: this is a string
regexp_lastIndex: 4
-------------------------- --
exec_array: ["is","i"]
exec_array_index: 5
exec_array_input: this is a string
regexp_lastIndex: 7
----------🎜>---------- ------------------
exec_array: null
regexp_lastIndex: 0
1.在沒有g標識符時,match和exec方法效果是一樣的;有g標識符時,exec方法可以提供最完整的匹配結果。
2.這裡順便提一下RegExp.test()方法,它是exec方法的簡化版,有匹配結果就回傳true,沒有匹配結果就回傳false,執行過程與exec是一樣的。相當於 (p.exec(s) != null)。
3.RegExp的lastIndex屬性在有g標識符,且在exec和test方法中是有效的,其他地方是無效的。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...
