Skip to content

Commit 999f44d

Browse files
committed
js/ree.a:1
1 parent 876428d commit 999f44d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

javascript/es6/regexp-emoji.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Matching Emoji in RegExp
2+
3+
`\p{Emoji}` + `u` flag
4+
5+
* Example:
6+
7+
```
8+
function countEmoji(input) {
9+
const c = input.match(
10+
/\p{Emoji}/gu
11+
);
12+
13+
return (c || []).length;
14+
}
15+
16+
17+
countEmoji('xd 😱🤠 hehe 🙃 🤕')
18+
// 4
19+
20+
countEmoji('źć auuu')
21+
// 0
22+
23+
countEmoji('x🐍x')
24+
// 1
25+
26+
countEmoji('👍')
27+
// 1
28+
```
29+
30+
## `\P{Emoji}`
31+
32+
To match all but emoji use `\P{Emoji}`:
33+
34+
```
35+
function leaveOnlyEmoji(input) {
36+
return input.replace(
37+
/\P{Emoji}/gu, ''
38+
);
39+
}
40+
41+
42+
leaveOnlyEmoji('xd 😱🤠 hehe 🙃 🤕')
43+
// '😱🤠🙃🤕'
44+
45+
leaveOnlyEmoji('źć auuu')
46+
// ''
47+
48+
leaveOnlyEmoji('x🐍x')
49+
// '🐍'
50+
51+
leaveOnlyEmoji('👍')
52+
// '👍'
53+
```

0 commit comments

Comments
 (0)