Hey so I need to create a script using googlespreadsheets (javascript) that takes the input of one cell and outputs all the emojis from that cell into the selected one. I want to do this by removing everything from the cell text except the emoji. This is because if I try to just match emojis my output is not correct.
I'm using this regex to locate emojis.
var re = /[\u1F60-\u1F64]|[\u2702-\u27B0]|[\u1F68-\u1F6C]|[\u1F30-\u1F70]|[\u2600-\u26ff]|[\uD83C-\uDBFF\uDC00-\uDFFF]+/gi;
How can I remove everything from the text except items with this regex. Or how can I remove everything but unicode. I have tried all the other suggestions but the output isn't correct or it doesn't work with spreadsheets.
Currently I have:
function SHOW_EMOJIS(s) {
var re = /[\u1F60-\u1F64]|[\u2702-\u27B0]|[\u1F68-\u1F6C]|[\u1F30-\u1F70]|[\u2600-\u26ff]|[\uD83C-\uDBFF\uDC00-\uDFFF]+/gi;
var result = s.match(re).toString();
return result;
}
This returns all the emojis, but instead of seeing: โ ๏ธ๐โ๏ธ๐ฉ๐ปโโ๏ธโ๏ธ๐ฅ I see โ ,๐,โ,๐ฉ๐ป,โ,โ,๐ฅ . The doctor is returned as two separate emoji-characters.
Instead of a custom function, Why not try the inbuilt REGEX?
=REGEXREPLACE(A5,"[[:print:]]","")
Emoji is not printable according to Google Re2๐
To replace all emojis from [A1] please try:
=REGEXREPLACE($A$1,"[๐ป๐ผ๐ฝ๐พ๐ฟยฉยฎโผโโขโนโ-โโฉ-โชโ-โโจโโฉ-โณโธ-โบโโช-โซโถโโป-โพโ-โโโโ-โโโโ โข-โฃโฆโชโฎ-โฏโธ-โบโโโ-โโ-โ โฃโฅ-โฆโจโปโพ-โฟโ-โโโ-โโ -โกโงโช-โซโฐ-โฑโฝ-โพโ-โ
โโ-โโโ-โโฉ-โชโฐ-โตโท-โบโฝโโ
โ-โโโโโโโกโจโณ-โดโโโโโ-โโโฃ-โคโ-โโกโฐโฟโคด-โคตโฌ
-โฌโฌ-โฌโญโญใฐใฝใใ๐๐๐
ฐ-๐
ฑ๐
พ-๐
ฟ๐๐-๐๐-๐๐๐ฏ๐ฒ-๐บ๐-๐๐-๐ก๐ค-๐๐-๐๐-๐๐-๐ฐ๐ณ-๐ต๐ท-๐บ๐-๐ฝ๐ฟ-๐ฝ๐-๐๐-๐ง๐ฏ-๐ฐ๐ณ-๐บ๐๐-๐๐๐-๐๐ค-๐ฅ๐จ๐ฑ-๐ฒ๐ผ๐-๐๐-๐๐-๐๐ก๐ฃ๐จ๐ฏ๐ณ๐บ-๐๐-๐
๐-๐๐-๐๐-๐ฅ๐ฉ๐ซ-๐ฌ๐ฐ๐ณ-๐ผ๐ -๐ซ๐ฐ๐ค-๐คบ๐คผ-๐ฅ
๐ฅ-๐งฟ๐ฉฐ-๐ฉด๐ฉธ-๐ฉผ๐ช-๐ช๐ช-๐ชฌ๐ชฐ-๐ชบ๐ซ-๐ซ
๐ซ-๐ซ๐ซ -๐ซง๐ซฐ-๐ซถ๐ฆ-๐ฟ#๏ธโฃ*๏ธโฃ0๏ธโฃ1๏ธโฃ2๏ธโฃ3๏ธโฃ4๏ธโฃ5๏ธโฃ6๏ธโฃ7๏ธโฃ8๏ธโฃ9๏ธโฃ]","")
Related Answer:
https://stackoverflow.com/a/70125203/5372400
=REGEXREPLACE(A5,"[[:print:]]","") is a nice solution, but have some drawbacks:
it leaves these chars: 0 1 2 3 4 5 6 7 8 9 # * on the place of respective emojis 0๏ธโฃ 1๏ธโฃ 2๏ธโฃ 3๏ธโฃ 4๏ธโฃ 5๏ธโฃ 6๏ธโฃ 7๏ธโฃ 8๏ธโฃ 9๏ธโฃ ๐ #๏ธโฃ *๏ธโฃ
it also replaces other not printable chars.
Test sheet
Related
I am trying to parse txt files with js + regex and my problem is as follows:
I have multiple txt files, and inside each one I need to search for an Id, made by 6 characters (numb + letters)
this is the string inside one of those files:
**IFCPROPERTYSINGLEVALUE('codice sito',$,IFCTEXT('I013FR'),$);**
I need to extract the I013FR only, and so far the closest js-regex I wrote is:
(codice sito',\$,IFCTEXT\('[a-zA-Z\d]{6})
using that, I get in return:
codice sito',$,IFCTEXT('I372TO
now I need to "add something" at the end of the regex, in order to only take the last 6 characters from the match.
Is that possible? am I on the right way? or maybe there is another better way to do that?
To extract the sequence of symbols, you need to put it in parenthesis. This pattern is called a "capturing group". Read more
/codice sito',\$,IFCTEXT\('([a-zA-Z\d]{6})/g
And then you can get your id using RegExp.exec() method.
const str = "**IFCPROPERTYSINGLEVALUE('codice sito',$,IFCTEXT('I013FR'),$);**";
const regex = /codice sito',\$,IFCTEXT\('([a-zA-Z\d]{6})/g;
const id = regex.exec(str)[1];
This question already has answers here:
Detect URLs in text with JavaScript
(15 answers)
Closed 4 years ago.
I'm looking at a 'to do' list application that uses JavaScript.
One of the functions converts text in to a hyperlink when https, https or ftp is present.
I'd like to expand this so if my text contains # followed by any 5 digit number, that becomes a link as well. The URL should be http://192.168.0.1/localsite/testschool/search.php?id=ID
where ID is the # and 5 digit number.
This is the current Javascript:
function prepareHtml(s)
{
// make URLs clickable
s = s.replace(/(^|\s|>)(www\.([\w\#$%&~\/.\-\+;:=,\?\[\]#]+?))(,|\.|:|)?(?=\s|"|<|>|\"|<|>|$)/gi, '$1$2$4');
return s.replace(/(^|\s|>)((?:http|https|ftp):\/\/([\w\#$%&~\/.\-\+;:=,\?\[\]#]+?))(,|\.|:|)?(?=\s|"|<|>|\"|<|>|$)/ig, '$1$2$4');
};
called using prepareHtml(item.title)
Any idea how I can do this ?
I've worked out regex to match the # and 5 digits is ^#([0-9]{5}) but I'm not sure how to implement this in the function.
Thanks
Looks to me that the pattern & replace string can be simplified a bit.
function urls2links(s)
{
return s.replace(/(^|[\s>])(www\.)/gi, '$1http://$2')
.replace(/\b(((https?|ftps?):\/{2})(\S+?))(?="|<|>|[\s<>\"]|$)/ig, '$1');
};
var str = 'blah http://192.168.0.1/localsite/testschool/search.php?id=#12345 \nblah www.foo.com/bar/" blah';
console.log('--\n-- BEFORE\n--');
console.log(str);
console.log('--\n-- AFTER\n--');
console.log(urls2links(str));
Although I'm not too sure about needing to include the character entities |"|<|> in the lookahead.
But I'm guessing you also deal with encoded strings.
This question already has answers here:
Regex doesn't omit quotes when matching text in quotes
(3 answers)
Closed 7 years ago.
I'm trying to apply a regular expression to a string, in search of any placeholders, but I can't figure out why the result is only the full match, not the capturing group.
//----HTML------------------------//
<p>Let's try plaintext.<br/>
bommdidumpidudidoo</p>
<p id="output"></p>
//----JS------------------------//
var s = $('p').html();
var matches = s.match( /.*(plaintext)/g );
write(matches);
write(matches[0]);
write(matches[1]);
//------- whatever -------//
function write(s) {
$('#output').html( $('#output').html() +'<br/>'+s );
}
// output:
// Let's try plaintext
// Let's try plaintext
// undefined
ยป fiddle
(I've used my custom function write instead of console.log so that the result would show up in the fiddle)
Why is the third line undefined? I don't understand!
I'm pretty sure the expression is right. I'm 1oo% certain that this is the right capturing group syntax for JavaScript, and that .match() returns an array with the full match first, then all capturing groups. I've even tested it with regex101.com โ over there, I do get the capturing group.
It's not the same as this other problem because there, the OR logic was the crux of the problem and here the pipe | doesn't even come up.
Oooh! It's because I'm doing a global search, with the g modifier. I do get the expected result if I remove that modifier. Tss.
I've used that modifier in the first place in order to grab multiple placeholders, but I guess I can still while-loop that stuff โฆ
I've got a simple issue I just can't seem to figure out. I am converting all text that starts with a # to be a link.
Everything works fine, my issue is I want it to convert ONLY if the word that starts with # is 3 characters or more (not counting the #).
EX.
I want it to convert these to a link:
#test #cool #stackoverflow
I don't want these to convert to links:
#ok #no #m
The function that replaces the words to link is below:
function linkHashtags(text) {
hashtag_regexp = /#([a-zA-Z0-9]+)/g;
return text.replace(hashtag_regexp,
'#$1');
}
Then I call the function like this:
$('.text p').each(function () {
$(this).html(linkHashtags($(this).html()));
});
Use the interval operator syntax
/#([a-zA-Z0-9]{3,})/g;
{3,} basically tells it to test for 3 or more characters
Currently I'm using jquery.maskedinput for verious mask formats, but it's not working for phone numbers.
In Brasil we used to have all numbers in the format (99)9999-9999. But lately, in a few cities, cell phones are using (99)99999-9999, while their normal phones and the rest of the country remain (99)9999-9999.
jquery.maskedinput seems to not support 2 formats on the same input where a character in the middle of the string may or may not be present. As I can see in its documentation, I could have (99)9999-9999 and (99)9999-99999, but that would confuse users.
Is there any other mask plugin/framework that allows me to validate both (99)9999-9999 and (99)99999-9999?
Edit: I created a full test using harry and Dmitrii solutions: http://jsfiddle.net/NSd6g/ $('#full').inputmask('(99)9999[9]-9999');
I'm gonna wait a bit more to see if I can find an even better solution. The perfect one wouldn't require the red message, second group would have 4 digits by default. And, if third group would get a fifth digit, only then second group would get its fifth space, moving third group's first digit into second group's fifth. lol kinda hard to understand, sorry!
You could achieve this using jquery.inputmask as simple as:
jQuery(function($) {
$('#test').inputmask('(99)9999[9]-9999');
});
Try this demo.
To skip the optional part while typing into the input you need to type space or the char following the optional part in the mask (hyphen in this case).
I'm Brazilian too.
At my job we don't actually use the "-" char in the middle for those type of masks, so there's no confusion... the final mask would be the following: (99)99999999?9
It's a bit harder to the final user to identify a wrongly typed phone number this way, but it works.
Another way I know is building the regex in JS and then using it with another plugin, like, for example, jQuery Validate.
With jQuery mask you can only have the trailing characters be optional. Not ones in the middle on the input.
My suggestion would be to have 3 input boxes, one for each part of the number with the optional character at the end of the middle input. Then concatenate the inputs on submit.
like so:
(<input id="phone2" />)<input id="phone3" />-<input id="phone4" />
jQuery(function($){
$("#phone2").mask("99");
$("#phone3").mask("9999?9");
$("#phone4").mask("9999");
});
See fiddle: http://jsfiddle.net/Rge83/1/
To make it more user friendly, a script to move to the next input once the current one has been filled can be added + some css to make the inputs look more like one.
My suggestion: "(99) [9] 9999-9999"
//Configuraรงรฃo para celular com nono digito
$('#Seletor').focusout(function () {
var phone, element;
element = $(this);
element.unmask();
phone = element.val().replace(/\D/g, '');
if (phone.length > 10) {
element.mask("(99) 99999-999?9");
} else {
element.mask("(99) 9999-9999?9");
}
}).trigger('focusout');
Some updates 10 years later:
jQuery is still alive (yes!)
all mobiles in Brazil have 9 digits now (besides 2 digits from area code)
all Brazilian landlines still have 8 digits (besides area code)
So in order to have a field accepting both mobiles or landlines I prefer a dynamic mask that will automatically adjust itself to "5+4" or "4+4" depending on how many digits were typed.
Example:
$('#ContactPhone')
// by default we expect a mobile (5+4 digits)
// so the initial mask splits 5+4 and if someone pastes 9 digits
// the separator position won't be changed causing a blip
.inputmask('(99) 99999-999[9]')
.on('change keyup paste', function () {
var digits = $(this).val().replace(/\D/g, '').length;
// ... if there are only 8 digits we reformat as 4+4 -
// but yet letting space for another digit (which would reformat the mask back to 5+4)
if (digits == 10)
$(this).inputmask('(99) 9999-9999[9]');
else
$(this).inputmask("(99) 99999-9999");
});