I am trying to make a function that takes a string and a delimiter and splits the string it into an array, while keeping the delimiter, using case insensitivity for the search, and preserving original case.
For example, the function signature should look like:
advanced_split("Test Round Start", "St")
And it should return:
["Te", "st", " Round ", "St", "art"]
Note that the splitting is done using case insensitivity, but the case in the original string is preserved in the output array.
This will do it.
function advanced_split(string, delimiter) {
return string.split(new RegExp(`(${delimiter})`, 'i'));
}
advanced_split("Test Round Start", "St") // ["Te", "st", " Round ", "St", "art"]
It uses a capturing group to extract the delimiter portion of each split, preserving the case. The i
flag to a regex means it will be case insensitive.
It's worth pointing out that to make this function more robust, you should use a regex quoting function on the delimiter, otherwise the function may crash on delimiter strings which have special characters in regexs.
<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: ...
<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: ...
I'm using the body-parser package like this: // For parsing application/json: app.use(require('body-parser').json()); // For parsing application/x-www-form-urlencoded app.use(require('body-parser')....
I'm using the body-parser package like this: // For parsing application/json: app.use(require('body-parser').json()); // For parsing application/x-www-form-urlencoded app.use(require('body-parser')....
I'm doing some analysis on a large base, mostly javascript (it's actually mostly Siebel eScript, but that's more or less the same thing, grammatically) Over the years, bad programmers have been in ...
I'm doing some analysis on a large base, mostly javascript (it's actually mostly Siebel eScript, but that's more or less the same thing, grammatically) Over the years, bad programmers have been in ...
I need to find a very performant way to find out if a custom element or any of its parent elements has display: none; First approach: checkVisible() { let parentNodes = []; let el = this; ...
I need to find a very performant way to find out if a custom element or any of its parent elements has display: none; First approach: checkVisible() { let parentNodes = []; let el = this; ...