0

I would like to find strings delimited with @ and whitespace. It contains Danish alphabet or 4 digits number.


In the text below, I want to match all of the characters between @ and whitespace.

Lorem @ipsum dolor sit amet consectetur adipisicing elit. Doloremque minus rem fugit @reprehenderit @velit voluptate voluptas ipsa quos deleniti beatae soluta, libero nisi @necessitatibus eius. @4000 Numquam, dolorum. Nihil, @dolorem animi @vokalforråd.


I want to match none of the words between @ and whitespace in this text.

Lorem@ipsum.com dolor sit, amet consectetur adipisicing elit. Sunt natus officiis tenetur repellat doloremque @ipsum-co quasi dicta explicabo eius, esse non eum Lorem@ipsum nostrum rerum. Vitae maiores velit esse porro placeat eveniet.

Info

Danish alphabet consists of English alphabet + æ,ø, and å.

https://en.wikipedia.org/wiki/Danish_and_Norwegian_alphabet

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
mahan
  • 12,366
  • 5
  • 48
  • 83
  • How about split loop trim add – Ullas Hunka Jul 18 '18 at 14:51
  • 1
    `I want to escape all of the strings in this text` -- that request is very ambiguous. What does "escape" mean to you in this context, and what are "all the strings"? Are you talking about all the matches again between `@` and whitespace? – Patrick Roberts Jul 18 '18 at 14:54
  • @PatrickRoberts. Yes. Updated the question. – mahan Jul 18 '18 at 15:03
  • https://stackoverflow.com/questions/18382460/handling-danish-special-characters – meistermuh Jul 18 '18 at 15:04
  • @meistermuh PHP != JavaScript. The regexes used in those answers are not valid in JavaScript. – Patrick Roberts Jul 18 '18 at 15:29
  • @PatrickRoberts Oh I see, sorry, you're right. Natively the "old" JavaScript doesn't. The "new" one does. The old one would need help like mentioned here: https://stackoverflow.com/a/13210296/7424979 – meistermuh Jul 19 '18 at 07:19

1 Answers1

2

I'll be happy to update my answer based on clarifications, but given your current criteria, @vokalforråd. should not be matched because it ends with . instead of whitespace.

const tests = ['Lorem @ipsum dolor sit amet consectetur adipisicing elit. Doloremque minus rem fugit @reprehenderit  @velit voluptate voluptas ipsa quos deleniti beatae soluta, libero nisi @necessitatibus eius. @4000 Numquam, dolorum. Nihil, @dolorem animi @vokalforråd.', 'Lorem@ipsum.com dolor sit, amet consectetur adipisicing elit. Sunt natus officiis tenetur repellat doloremque @ipsum-co quasi dicta explicabo eius, esse non eum Lorem@ipsum nostrum rerum. Vitae maiores velit esse porro placeat eveniet.']
const pattern = /(?:^|\s)@[\da-zæøå]+(?=\s)/gi

tests.forEach(test => {
  console.log(test.match(pattern))
})

You'll need to trim the leading whitespace, if any, from each of the matches with this particular pattern. You can do that with string.trim().

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • Thanks. But I do not want to match `Lorem@ipsum`. I only want to match a word if there is a space before `@` and a space after of it (word). – mahan Jul 18 '18 at 15:21