Skip to content

Commit f5460a1

Browse files
committed
Add replaceMatches method
1 parent dec27c8 commit f5460a1

File tree

2 files changed

+138
-104
lines changed

2 files changed

+138
-104
lines changed

ExSwift/String.swift

+18-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public extension String {
114114
}
115115

116116
/**
117-
Check is string with this paaternt included in string
117+
Check is string with this pattern included in string
118118

119119
:param: pattern Pattern to match
120120
:param: ignoreCase true for case insensitive matching
@@ -129,6 +129,23 @@ public extension String {
129129
return nil
130130
}
131131

132+
/**
133+
Replace all pattern matches with another string
134+
135+
:param: pattern Pattern to match
136+
:param: replacementString string to replace matches
137+
:param: ignoreCase true for case insensitive matching
138+
:returns: true if contains match, otherwise false
139+
*/
140+
func replaceMatches (pattern: String, withString replacementString: String, ignoreCase: Bool = false) -> String? {
141+
if let regex = ExSwift.regex(pattern, ignoreCase: ignoreCase) {
142+
let range = NSMakeRange(0, count(self))
143+
return regex.stringByReplacingMatchesInString(self, options: .allZeros, range: range, withTemplate: replacementString)
144+
}
145+
146+
return nil
147+
}
148+
132149
/**
133150
Inserts a substring at the given index in self.
134151

0 commit comments

Comments
 (0)