I would like insert dashes to ID based on another property.
In the property the user can include dashes anywhere. The IDs should display in the format given in the property.
For example:
property: 111-11-1111
The ID should display like 123-11-1111.
Something similar where we will add dashes from property:
id.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
Here's one solution. It splits the format string on dashes. Then uses the lengths of those substrings to pull substrings from the id. Then joins those with dashes.
const mapID = (id, format) => {
return format.split('-')
.map((ones, idx, onesArray) => id.substr(onesArray.slice(0, idx).join('').length, ones.length))
.join('-');
}
console.log(mapID("123456789", "111-11-1111"));
console.log(mapID("123456789", "1111-1-11-11"));
Related
I have a string with a lot of random words: example var stringWithWords = "hello my name is" the separator between the words are a space.
I have also a variable var word1 = "name". (BUT its dynamic, it can change and I dont know exactly where it is in the string above).
Now the question: How can I remove word1 from the string stringWithWords ?
Try this:
array = array.map(x => {
return x.replace(name, '').trim();
})
That's for an array, i think you changed your question since I first read it tho...
For a string just use replace and then trim to remove unnecessary whitespace...
I want to scan an website and filter out the most repeating keywords in the site.
If i would scan for example https://www.adidas.at/ then i would like to have an array with keywords like:
["clothing", "shoes", "pants", "t-shirt"]
and so on.
My idea was first to get all text content witch is inside of <body> with $eval() and split it up into an array and the use .reduce() and count how many times an word appears on the site and rank it.
Is this the way to go or is there an simple solution for it?
I would say the best approach would be to use page.evaluate(), and inside, acquire outerText of body which would return all the text content without the tags and stuff. After which you can convert the entire text to lower case(using toLowerCase()), and split the string on whitespaces or linebreaks or certain other symbols like brackets, etc. You can them keep count of repeated words using an object to store key values of word-count. So in essence your code would look like this:
let data = await page.evaluate(() => {
//acquire the text in body
let content = document.querySelector("body").outerText;
//convert text to lowercase and split on whitespaces and newlines
let textArray = content.toLowerCase().replace(/[^A-Za-z0-9]/gm, " ").split(/\s+/gm);
//object to map text to count
let text_to_count_map = {};
textArray.forEach(t => {
if(text_to_count_map[t])
text_to_count_map[t] ++;
else
text_to_count_map[t] = 1;
});
return text_to_count_map;
})
Now your data variable will contain an object of each word in the body as the key whose corresponding value would be their frequency in the body. You can do with it whatever you wish.
EDIT 1
Tested this on the Adidas site, and able to acquire all the words in the body into an array for textArray variable.
FINAL EDIT
Used a simpler regexp to filter anything that is not containing letters ([^A-Za-z0-9])
I have a string or I can make it an array of strings. and when I try to filter it using includes if there are more than one search terms it doesn't return anything where is my mistake?
villas() {
return this.$store.state.villas.filter((villa) => {
return villa.f.includes(this.toFilter)
});
}
here is toFilter is the array of search terms
and f is the string i make the searching
So I get your component has an array of "villas". Each of them has an f property.
There's also an array this.toFilter which contains a whitelist of words (search terms) and you want to filter the villas whose f property match the search terms.
If f was a string (like a description), a positive match would be when a string includes one of the search terms.
If f was an array (like, the tags of the villa) a positive match would be when there is an non-empty intersection between the tags and the search terms.
Fortunately both strings and arrays have a includes method, so the following should work either way:
villas() {
return this.$store.state.villas.filter((villa) => {
return this.toFilter.filter(term=>{
return villa.f.includes(term);
}).length>0;
});
}
I think you use the wrong syntax. If "this.toFilter" is an array and "f" is a string
You should use: this.toFilter.includes(villa.f)
includes
User list array filtering by containing the tag. I have an array of user list and I want to filter by tag contain using array filter match and RegExp for matching contain text its work but not get expected result.
let users=[{id:1,name:'john',tags:'a,b,c,v'},{id:2,name:'die',tags:'a,b,w,x'},{id:3,name:'ren',tags:'c,p,q,n'}];
let tagString='a,b,c';
let tagStringQuery = new RegExp(tagString, "i");
let data=users.filter((user) => user.tags.match(tagStringQuery)).map((user)=> user);
console.log('data',data);
O/P = [{id:1,name:'john',tags:'a,b,c,v'}]
but expected result is all user list which contains an 'a' or 'b' or 'c' tag.
This is really not a job for a regexp. Ideally you would have tags as Set instances, using intersection with a query Set to check for presence of tags.
If you insist on regexp though, you can't directly search for a,b,c to find if any of a, b or c is present: you would need to search for a|b|c (i.e. tagString.split(',').join('|')). If tags are of more than one letter, then you need to worry about substrings, so the search string should be \b(a|b|c)\b, and you should regexp-escape all the strings. And if you have multi-word tags or weird characters in them, you would need to search for ,(a|b|c), inside "," + user.tags + ",".
tagstring has to be 'a|b|c' instead of 'a,b,c' in case you want to apply or operator to those tags
Something like this should work fine without regex
let data = users.filter(x=>
tagString
.split(",")
.some(r=>
x.tags.split(",").includes(r)));
I want to convert given Unicode Chars into Emojis.
From a function, I get a string and sometimes there are emojis in it but as Unicode (like this \ud83c\uddee\ud83c\uddf9).
So I need to check first if this functions contains these Unicode emoji chars and then I need to convert them into emojis.
With this line of code, I tried to remove these Unicode chars, but it doesn't work.
But now I need to find a method to convert these Unicode chars into Emojis and not removing them!
var fullnameWOE = fullname[1].replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, '')
EDIT: still found no method to solve this problem...
I have to add that I'm getting this string with name containing emoji from a php file, so if there are any opportunities to use php to solve it?
just refere to this one How to find whether a particular string has unicode characters (esp. Double Byte characters) to check if unicodes are present.
But in order to replace with emoticons I suggest you to use a dictionary because it will be more controlled.
const raw = 'This string could contain an emoticon: {{example of emoticon unicode}}';
const EMOJS = {
'example of emoticon unicode': 'REPLACED WITH CORRESPONDING VALUE'
};
function compile(input, dict = EMOJS) {
return Object
.keys(dict)
.reduce(
(res, emojId) => {
let tmp;
do {
tmp = res;
res = res.replace(emojId, dict[emojId]);
} while(tmp !== res);
return res;
},
input
)
}
console.log({input: raw, output: compile(raw)});
here you go:
function containsNonLatinCodepoints(s) {
return /[^\u0000-\u00ff]/.test(s);
}