在 JavaScript 中拆分大写字母的字符串
Split a String on Capital Letters using JavaScript
使用正则表达式调用该split()
方法以拆分大写字母的字符串,例如str.split(/(?=[A-Z])/);
.
正则表达式使用肯定的先行断言来拆分每个大写字母上的字符串。
const str = 'BobbyHadzCom'; const result = str.split(/(?=[A-Z])/); // 👇️ [ 'Bobby', 'Hadz', 'Com' ] console.log(result);
我们传递给String.split方法的唯一参数
是一个正则表达式。
正斜杠标记/ /
正则表达式的开始和结束。
处理尾随空格
如果您的字符串包含空格,则数组中的子字符串将包含尾随空格。
const str = 'Bobby Hadz Com'; const result = str.split(/(?=[A-Z])/); // 👇️ [ 'Bobby ', 'Hadz ', 'Com' ] console.log(result);
同样,如果字符串以空格开头,您将得到一个包含空格的数组元素。
const str = ' Bobby Hadz Com '; const result = str.split(/(?=[A-Z])/); // 👇️ [ ' ', 'Bobby ', 'Hadz ', 'Com ' ] console.log(result);
我们可以使用
String.trim()
和
Array.map()
方法来处理这些边缘情况。
const str = ' Bobby Hadz Com '; const result = str .trim() .split(/(?=[A-Z])/) .map(element => element.trim()); // 👇️ [ 'Bobby', 'Hadz', 'Com' ] console.log(result);
我们使用该trim()
方法从原始字符串中删除任何前导和尾随空格。
// 👇️ "abc" console.log(' abc '.trim());
然后我们调用该split()
方法在每个大写字母上拆分字符串。
最后,我们使用该Array.map()
方法从数组中的字符串中删除任何前导或尾随空格。
map()
被数组中的每个元素调用。On each iteration, we trim the string to remove the leading and trailing spaces
and return the result.
The Array.map
method returns a new array that contains the values we returned
from the callback function.
If you have to do this often, define a reusable function.
function splitOnCapital(str) { return str .trim() .split(/(?=[A-Z])/) .map(element => element.trim()); } const str = ' Bobby Hadz Com '; const result = splitOnCapital(str); console.log(result); // 👉️ [ 'Bobby', 'Hadz', 'Com' ]
The splitOnCapital
function takes a string and splits it on each capital
letter.
Split a String on Capital Letters using String.match()
#
Alternatively, you can use the String.match()
method.
function splitOnCapital(str) { return str .trim() .match(/([A-Z]?[^A-Z]*)/g) .map(element => element.trim()) .filter(element => element); } const str = ' Bobby Hadz Com '; const result = splitOnCapital(str); console.log(result); // 👉️ [ 'Bobby', 'Hadz', 'Com' ]
The
String.match
method matches a string against a regular expression.
The method returns an array containing the matches (if any) or null
if no
matches are found.
The forward slashes / /
mark the beginning and end of the regular expression.
The [A-Z]
character class matches the capital letters from A
to Z
.
function splitOnCapital(str) { return str .trim() .match(/([A-Z]?[^A-Z]*)/g) .map(element => element.trim()) .filter(element => element); }
The question mark ?
matches the preceding item (the capital letters) 0 or 1
times.
The caret ^
symbol means “NOT the following”.
So, the [^A-Z]
character class matches any character that is not a capital
letter.
The asterisk *
matches the preceding item (non-capital letters) zero or more
times.
The g
flag is used because we want to match all occurrences of the regular
expression and not just the first occurrence.
If you ever need help reading a regular expression, check out this
regular expression cheatsheet
by MDN.
It contains a table with the name and the meaning of each special character with
examples.