Combining a filter and a map - javascript

Is it possible to combine a map and a filter in a single javascript expression? For example, I am currently doing the following to trim whitespace and remove empty results:
const s = "123 hiu 234234"
console.log(s
.split(/\d+/g)
.filter((item, i) => item.trim())
.map((item, i) => item.trim())
);
Is there a more compact way to do that? And, as a follow up question, is the /g necessary when doing split or does that automatically split every occurrence?

that...
const s = "123 hiu 234234"
console.log( s.match(/[a-z]+/ig) )

If a single word comes between the numbers, I think a single regular expression would be enough here - just match non-whitespace, non-digits:
const s = "123 hiu 234234"
console.log(s
.match(/[^\d ]+/g)
);

One way to do it would also be to define a function that takes two arguments that you can call which does the task. For example:
const s = "123 hiu 234234"
const Trim = (item, i) => item.trim();
console.log(s.split(/\d+/).filter(Trim).map(Trim));
Or you could put the burden on the regex itself, for example only matching letters:
s="123 hiu 234234"
console.log(s.match(/[a-zA-Z]+/g));
// /[a-z]+/gi alternately

Related

regex get by multiple separator?

I want separate the sentence
hey ! there you are
to
["hey!","there you are"]
in js. now I found that
(?<=\!)
keep separator with before element.
but what if I want to use the rule to the "!!" or "!!!"?
so my goal is change to separate sentence from
hey! there!!! you are!!!!
to
["hey!","there!!!", "you are!!!!"]
but is it possible?
I tried to \(?<=!+) or \(?<=\+!) but fail.
I don't know even it possible to get !, !!..n by once
In addition to the elegant solution by The fourth bird, this specific requirement can be met by simply splitting on the regex, (?<=!)\s+|$ which can be explained as "One or more whitespace characters, or end of line, preceded by a !".
const regex = /(?<=!)\s+|$/;
[
"hey! there!!! you are!!!!",
"hey ! there you are"
].forEach(s =>
console.log(
s.split(regex)
.map(s => s.replace(/\s+!/, "!").trim())
)
);
Based on your needs my solution was to first get the exclamations, then get the strings (split by exclamations).
This method creates two arrays, one of exclamations and one of the strings.
Then I just loop over them, concatenate them, and push into a new array.
As a starting point it should be enough, you can always modify and built on top of this.
const str = 'hey! there!!! you are!!!!';
const exclamations = str.match(/!+/g);
const characters = str.split(/!+/).filter(s => s.trim());
let newArr = [];
for (i = 0; i < characters.length; i++) {
newArr.push(characters[i].trim() + exclamations[i]);
}
console.log(newArr); // ["hey!","there!!!","you are!!!!"]
You could use split with 2 lookarounds, asserting ! to the left and not ! to the right. If you want to remove the leading whitespace chars before the exclamation mark you could do some sanitizing:
const regex = /(?<=!)(?!!)/g;
[
"hey! there!!! you are!!!!",
"hey ! there you are"
].forEach(s =>
console.log(
s.split(regex)
.map(s => s.replace(/\s+!/, "!").trim())
)
);
Another option could be to match the parts instead of splitting:
[^\s!].*?(?:!(?!!)|$)
See a regex demo.
const regex = /[^\s!].*?(?:!(?!!)|$)/g;
[
"hey! there!!! you are!!!!",
"hey ! there you are"
].forEach(s =>
console.log(s.match(regex))
);
Here is yet another solution using a .split() and .reduce(). This does not use a lookbehind, for those concerned about Safari and other browsers not supporting it:
[
'hey ! there you are',
'hey! there!!! you are!!!!'
].forEach(str => {
let result = str
.split(/( *!+ *)/) // split and keep split pattern because of parenthesis
.filter(Boolean) // filter out empty items
.reduce((acc, val, idx) => {
if(idx % 2) {
// !+ split pattern => combine with previous array item
acc[acc.length - 1] += val.trim();
} else {
acc.push(val);
}
return acc;
}, []);
console.log(str, ' => ', result);
});
Output:
hey ! there you are => [
"hey!",
"there you are"
]
hey! there!!! you are!!!! => [
"hey!",
"there!!!",
"you are!!!!"
]

Is there a way to remove a newline character within a string in an array?

I am trying to parse an array using Javascript given a string that's hyphenated.
- foo
- bar
I have gotten very close to figuring it out. I have trimmed it down to where I get the two items using this code.
const chunks = input.split(/\ ?\-\ ?/);
chunks = chunks.slice(1);
This would trim the previous input down to this.
["foo\n", "bar"]
I've tried many solutions to get the newline character out of the string regardless of the number of items in the array, but nothing worked out. It would be greatly appreciated if someone could help me solve this issue.
You could for example split, remove all the empty entries, and then trim each item to also remove all the leading and trailing whitespace characters including the newlines.
Note that you don't have to escape the space and the hyphen.
const input = `- foo
- bar`;
const chunks = input.split(/ ?- ?/)
.filter(Boolean)
.map(s => s.trim());
console.log(chunks);
Or the same approach removing only the newlines:
const input = `- foo
- bar`;
const chunks = input.split(/ ?- ?/)
.filter(Boolean)
.map(s => s.replace(/\r?\n|\r/g, ''));
console.log(chunks);
Instead of split, you might also use a match with a capture group:
^ ?- ?(.*)
The pattern matches:
^ Start of string
?- ? Match - between optional spaces
(.*) Capture group 1, match the rest of the line
const input = `- foo
- bar`;
const chunks = Array.from(input.matchAll(/^ ?- ?(.*)/gm), m => m[1]);
console.log(chunks);
You could loop over like so and remove the newline chars.
const data = ["foo\n", "bar"]
const res = data.map(str => str.replaceAll('\n', ''))
console.log(res)
Instead of trimming after the split. Split wisely and then map to replace unwanted string. No need to loop multiple times.
const str = ` - foo
- bar`;
let chunks = str.split("\n").map(s => s.replace(/^\W+/, ""));
console.log(chunks)
let chunks2 = str.split("\n").map(s => s.split(" ")[2]);
console.log(chunks2)
You could use regex match with:
Match prefix "- " but exclude from capture (?<=- ) and any number of character different of "\n" [^\n]*.
const str = `
- foo
- bar
`
console.log(str.match(/(?<=- )[^\n]*/g))
chunks.map((data) => {
data = data.replace(/(\r\n|\n|\r|\\n|\\r)/gm, "");
return data;
})
const str = ` - foo
- bar`;
const result = str.replace(/([\r\n|\n|\r])/gm, "")
console.log(result)
That should remove all kinds of line break in a string and after that you can perform other actions to get the expected result like.
const str = ` - foo
- bar`;
const result = str.replace(/([\r\n|\n|\r|^\s+])/gm, "")
console.log(result)
const actualResult = result.split('-')
actualResult.splice(0,1)
console.log(actualResult)

How to split a camel case string containing numbers

I have camel cased strings like this:
"numberOf40"
"numberOf40hc"
How can I split it like this?
["number", "Of", "40"]
["number", "Of", "40hc"]
I am using humps to decamelize keys so I can only pass a split regex as option. I am looking for an answer that only uses split function.
My best attempts:
const a = "numberOf40hc"
a.split(/(?=[A-Z0-9])/)
// ["number", "Of", "4", "0hc"]
a.split(/(?=[A-Z])|[^0-9](?=[0-9])/)
// ["number", "O", "40hc"]
Also I don't understand why the f is omitted in my last attempt.
You don't get the f in you last attempt (?=[A-Z])|[^0-9](?=[0-9]) as this part of the last pattern [^0-9] matches a single char other than a digit and will split on that char.
You could also match instead of split
const regex = /[A-Z]?[a-z]+|\d+[a-z]*/g;
[
"numberOf40",
"numberOf40hc"
].forEach(s => console.log(Array.from(s.matchAll(regex), m => m[0])));
Using split only, you could use lookarounds with a lookbehind which is gaining more browser support.
const regex = /(?=[A-Z])|(?<=[a-z])(?=\d)/g;
[
"numberOf40",
"numberOf40hc"
].forEach(s => console.log(s.split(regex)));
const a = "numberOf40hc"
let b = a.split(/(\d+[a-z]*|[A-Z][a-z]*)/).filter(a => a);
console.log(b);
The .filter(a => a) is necessary because split, returns both the left and right side of a matched delimter. E.g. 'a.'.split('.') returns both the left (i.e. 'a') and right (i.e. '') side of '.'.
Per your update regarding the need for compatibility with humps, it seems humps supports customizing the handler:
const humps = require('humps');
let myObj = {numberOf40hc: 'value'};
let decamelizedObj = humps.decamelizeKeys(myObj, key =>
key.split(/(\d+[a-z]*|[A-Z][a-z]*)/).filter(a => a).join('_'));
console.log(decamelizedObj);
Try this:
const splitStr = (str='') =>
str.split(/([A-Z][a-z]+)/).filter(e => e);
console.log( splitStr("numberOf40") );
console.log( splitStr("numberOf40hc") );

How to match two non-consecutive words in a String for React/Javascript search?

I have a search bar which relies on this filter method.
I concatenate all the search strings in a variable concat, and then use either .includes() or .match() as shown below. If searched for multiple words, this only returns a result if the words occur consecutively in concat.
However, I want it to match ANY two words in concat, not just consecutive ones. Is there a way to do this easily?
.filter((frontMatter) => {
var concat =
frontMatter.summary +
frontMatter.title +
frontMatter.abc+
frontMatter.def+
frontMatter.ghi+
frontMatter.jkl;
return concat.toLowerCase().match(searchValue.toLowerCase());
});
Also tried;
.filter((frontMatter) => {
const concat =
frontMatter.summary +
frontMatter.title +
frontMatter.abc+
frontMatter.def+
frontMatter.ghi+
frontMatter.jkl;
return concat.toLowerCase().includes(searchValue.toLowerCase());
});
Thanks!
Everything is explained in the comments of the code.
If you don't care that "deter" matches the word "undetermined"
.filter((frontMatter) => {
// Get the front matter into a string, separated by spaces
const concat = Object.values(frontMatter).join(" ").toLowerCase();
// Look for a string in quotes, if not then just find a word
const regex = /\"([\w\s\\\-]+)\"|([\w\\\-]+)/g;
// Get all the queries
const queries = [...searchValue.toLowerCase().matchAll(regex)].map((arr) => arr[1] || arr[2]);
// Make sure that every query is satisfied
return queries.every((q) => concat.includes(q));
});
If you DO care that "deter" should NOT match the word "undetermined"
.filter((frontMatter) => {
// Get the front matter into a string, separated by spaces
// The prepended and appended spaces are important for the regex later!
const concat = ` ${Object.values(frontMatter).join(" ").toLowerCase()} `;
// Look for a string in quotes, if not then just find a word
const regex = /\"([\w\s\\\-]+)\"|([\w\\\-]+)/g;
// Get all the queries
const queries = [...searchValue.toLowerCase().matchAll(regex)].map((arr) => arr[1] || arr[2]);
// Make sure that every query is satisfied
// [\\s\\.?!_] and [\\s\\.?!_] check for a space or punctuation at the beginning and end of a word
// so that something like "deter" isn't matching inside of "undetermined"
return queries.every((q) => new RegExp(`[\\s\\.?!_]${q}[\\s\\.?!_]`).test(concat));
});
I'd use .reduce to count up the number of matches, and return true if there are at least 2:
const props = ['summary', 'title', 'abc', 'def', 'ghi', 'jkl'];
// ...
.filter((frontMatter) => {
const lowerSearch = searchValue.toLowerCase();
const matchCount = props.reduce(
(a, prop) => a + lowerSearch.includes(frontMatter[prop].toLowerCase()),
0
);
return matchCount >= 2;
})

Could a regular expression be used to find text between pairs of delimiters

I need to parse an email template for custom variables that occur between pairs of dollar signs, e.g:
$foo$bar$baz$foo$bar$baz$wtf
So I would want to start by extracting 'foo' above, since it comes between the first pair (1st and 2nd) of dollar signs. And then skip 'bar' but extract 'baz' as it comes between the next pair (3rd and 4th) of dollar signs.
I was able to accomplish this with split and filter as below, but am wondering, if there's a way to accomplish the same with a regular expression instead? I presume some sort of formal parser, recursive or otherwise, could be used, but that would seem like overkill in my opinion
const body = "$foo$bar$baz$foo$bar$baz$wtf";
let delimitedSegments = body.split('$');
if (delimitedSegments.length % 2 === 0) {
// discard last segment when length is even since it won't be followed by the delimiter
delimitedSegments.pop();
}
const alternatingDelimitedValues = delimitedSegments.filter((segment, index) => {
return index % 2;
});
console.log(alternatingDelimitedValues);
OUTPUT: [ 'foo', 'baz', 'bar' ]
Code also at: https://repl.it/#dexygen/findTextBetweenDollarSignDelimiterPairs
Just match the delimiter twice in the regexp
const body = "$foo$bar$baz$foo$bar$baz$wtf";
const result = body.match(/\$[^$]*\$/g).map(s => s.replace(/\$/g, ''));
console.log(result);
You could use this regex /\$\w+\$/g to get the expected output'
let regex = /\$\w+\$/g;
let str = '$foo$bar$baz$foo$bar$baz$wtf';
let result = str.match(regex).map( item => item.replace(/\$/g, ''));
console.log(result);
You can use capturing group in the regex.
const str1 = '$foo$bar$baz$foo$bar$baz$wtf';
const regex1 = /\$(\w+)\$/g;
const str2 = '*foo*bar*baz*foo*bar*baz*wtf';
const regex2 = /\*(\w+)\*/g;
const find = (str, regex) =>
new Array(str.match(regex).length)
.fill(null)
.map(m => regex.exec(str)[1]);
console.log('delimiters($)', JSON.stringify(find(str1, regex1)));
console.log('delimiters(*)', JSON.stringify(find(str2, regex2)));

Categories

Resources