0

I want to find match all the strings which will have emojis in the form of \u codes. I'm trying with below regexp but is not working.

/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g

But, it is not detecting. I want to match and get

\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04

these type of characters.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
user515
  • 45
  • 7
  • 1
    Where's you input string? And how should be your expected output? – cнŝdk Mar 22 '18 at 10:11
  • I don't know about the unicode codepoints, but your regex could be written in a single character class which would include multiple ranges and single codepoints (e.g. `[\uE000-\uF8FF\uD83C\uDC00-\uDFFF ... ]`) – Aaron Mar 22 '18 at 10:13
  • I think you should protect '\' with a backslash character, first. Don't know what's the input string and what's the expected output, but maybe this helps: `/(\\u[a-zA-Z0-9]{4})/g` – standby954 Mar 22 '18 at 10:16

1 Answers1

1

Well if you want to match emojis in the format \uXXXX using Regex, you can use this Regex:

/\\u[a-z0-9]{4}/gi

This is a simple Demo:

const regex = /\\u[a-z0-9]{4}/gi;
const str = `This is a pragraph \\ud83d having  some emojis like these ones:

\\ude04

\\ud83d

\\ude04

Have you seen them?

\\ud83d\\ude04

Great!

`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

The regex you wrote won't work because you were not escaping the \.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Thanks for the response but I'm gettting spaces between the emojis. https://jsfiddle.net/ofr41m8h/1/. I have added an example here. But they are not rendering as emojis.I will get like "\u" format but they are rendering in my browser. – user515 Mar 22 '18 at 10:44
  • @user515 I don't get it? Where are seeing the spaces? – cнŝdk Mar 22 '18 at 11:04