Emoji regex reuse

I am creating a simple chat with some basic emojis. So when the user, for example, types :)

, an emoji is displayed in the input field.

However, my regex fails when it comes to emoji :/

and the reason is that it messed up urls. (Keep in mind that emoji detection is triggered on the keyboard).

Sample test text:

Hello, could you please visit my site at http://www.example.com or https://www.example.com : /: //

So, for summation, the regex should not substitute :/

when it starts with http

or https

.

Currently in my collation I have this: "[^http?s]:/{1}(?!/)": "1F615"

but for some strange reason it continues to "have the previous character on replacement".

+3


source to share


2 answers


This should work

function reverse(s){
    return s.split("").reverse().join("");
}
function getMatch(str){
	const regex = /\/+\:(?!s{0,1}ptth)/g;
	const subst = ``;
	const result = reverse(reverse(str).replace(regex, subst));
	console.log(result);
}


getMatch(`Hello could you please visit my website at http://www.example.com or https://www.example.com :/ ://`);
      

Run codeHide result




Since Javascript does not support negative lookbehind, but does support negative images, we use this to our advantage by changing the string, replacing and reversing the direction

demo for modified string

0


source


Since the problem is that :/

will mess up http://

and JavaScript does not support lookbehinds ("check if it happened before or didn't happen"), I think the simplest solution would be to just check if there is :/

(not) and then another/

":/(?!/)" : "1F615"

      



Notably, the lookahead (which is supported by JavaScript) doesn't actually match /

what follows , it just makes sure it's not there.

+1


source







All Articles