0

I have a text like this:

let text = ":wink: Is that true? :like: I never heard about that, as I know some country like: VN, Indonesia, ThaiLan".

:wink: and :like: is code to display emoji. Now i use split to covert into array like this:

const myArray = text.split(':')

and result:

myArray = ["", "wink", " Is that true? ", "like", " I never heard about that, as I know some country like", " VN, Indonesia, ThaiLan"]

as you can see, character ':' is gone, how to keep ':' when covert from text to array, in this case my expect the result should be:

myArray = [":wink:", " Is that true? ", ":like:", " I never heard about that, as I know some country like:", " VN, Indonesia, ThaiLan"]
Hoàng Vũ Anh
  • 647
  • 1
  • 8
  • 24
  • 3
    Is that array really your goal, or is your goal to replace `:wink:` and `:like:` with emojis? (Building that array probably isn't the right way to do the latter.) – T.J. Crowder Aug 13 '18 at 09:59
  • Do you want to split off the `VN, Indonesia, ThaiLan` at the end? – Luca Kiebel Aug 13 '18 at 10:01
  • 1
    https://stackoverflow.com/questions/51378198/show-mix-image-from-url-and-text-in-one-view-react-native This is my main question :D – Hoàng Vũ Anh Aug 13 '18 at 10:03

1 Answers1

1

Try using match instead: match colons followed by non-colons followed by a final colon, OR match anything up until a point that's followed by :word::

let text = ":wink: Is that true? :like: I never heard about that, as I know some country like: VN, Indonesia, ThaiLan";
console.log(text.match(
  /:\w+:|.+?(?=:\w+:|$)/g
));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 1
    This removes the colon from `country like:`, which may not be OK. – Peter B Aug 13 '18 at 10:02
  • If text like this: **:tongue-out:sdasd:unlike:dsad:sleepy:dad** result is: **[":tongue-out", ":sdasd:", "unlike", ":dsad:", "sleepy:dad"]** and it's not display emoji. – Hoàng Vũ Anh Aug 13 '18 at 10:41