! Content: 正規表示式、可用的方法
正規表示式
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform pattern-matching and “search-and-replace” functions on text
首先先訂定一個pattern - /pattern/modifiers
1 | let re; |
可以運用的方法
exec() - Return result in an array or null
is used to retrieve the match of a regular expression in a string.
這個pattern是否有在這個字串裡,有則回陣列,無則回null
1 | const result = re.exec('hello world') |
test() - Return true or false
1 | const result = re.test('Hello') |
match() - Return result array or null
retrieves the specified value within a string or finds a match for one or more regular expressions.
這個字串是否有符合這個pattern,有則回陣列,無則回null
1 | const str = 'Hello There'; |
search() - Return index of the first match if not found return -1
1 | const str = 'dd Hello There' |
replace() - Return new string with some or all matchs of a pattern
1 | const str = 'Hello There' |