Preserving Delimiters When Splitting Strings in JavaScript with RegExp
Splitting a string into segments using regular expressions (RegExp) is a common task in JavaScript. However, sometimes it's desirable to preserve the delimiters used for splitting.
Consider the following string:
aaaaaa<br />† bbbb<br />‡ cccc
Suppose you wish to split this string at the
delimiter, which is followed by a special character († or ‡). The following RegExp can be used:
string.split(/<br \/>&#?[a-zA-Z0-9]+;/g);
While this RegExp accomplishes the splitting task, it discards the
delimiter. This can be undesirable in certain scenarios.
To retain the delimiters, various options can be employed, as demonstrated below:
string.split(/(<br \/>&#?[a-zA-Z0-9]+;)/g);
By enclosing the delimiter in parentheses, the delimiters are captured as separate elements in the resulting array.
Another approach involves using a positive lookahead assertion:
string.split(/(?=<br \/>&#?[a-zA-Z0-9]+;)/g);
This assertion matches the position immediately before the delimiter without actually consuming the delimiter.
Alternatively, a negative lookbehind assertion can be used:
string.split(/(?!<br \/>&#?[a-zA-Z0-9]+;)/g);
This assertion matches the position immediately after the delimiter without consuming it.
Additionally, a regular expression can be used to capture the portion of the string before the delimiter:
string.split(/(.*?<br \/>&#?[a-zA-Z0-9]+;)/g);
This captures everything up to the delimiter.
When dealing with paths, a special consideration is required to preserve directory slashes:
var str = 'Animation/rawr/javascript.js'; var tokens = str.match(/[^\/]+\/?|\//g);
This RegExp ensures that slashes following directories are also captured.
The above is the detailed content of How Can I Preserve Delimiters When Splitting Strings in JavaScript Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!