0

I've got a bunch of strings to browse and find there all words which contains "(at)" characters and then gather them in the array.

Sometimes is a replacement of "@" sign. So let's say my goal would be to find something like this: "account(at)example.com".

I tried this code:

let gathering = myString.match(/(^|\.\s+)((at)[^.]*\.)/g;);

but id does not work. How can I do it?

I found a regex for finding email addresses in text:

/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)

I think about something similar but unfortunately I can't just replace @ with (at) here.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
ampher911
  • 81
  • 1
  • 8
  • @gurvinder372 You are using unescaped parentheses in your pattern, and it won't behave as desired. – Tim Biegeleisen Apr 16 '18 at 06:16
  • @TimBiegeleisen Agree, though OP hasn't mentioned if `()` will only be around `at` or can it be mentioned anywhere else as well or not. – gurvinder372 Apr 16 '18 at 06:20
  • @gurvinder372 Hey I was thinking about whole set of chars _(at)_ in the same place, inside a word. It dosen't matter if _()_ will appear somewhere else in the string. – ampher911 Apr 16 '18 at 06:31
  • I found a regex for finding email addresses in text: `/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)` I think about something similar but unfortunately I can't just replace "@" with "(at)" here :) – ampher911 Apr 16 '18 at 06:33
  • You have a working regex with `@`, and replacing it with correctly escaped `(at)` will solve the issue. You just need to escape `(` and `)` to match these chars literally. – Wiktor Stribiżew Apr 16 '18 at 06:48
  • @Wiktor Stribiżew you mean something like: `/([a-zA-Z0-9._-]+(at)[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)` ? I don't know exactly how to specify this set of chars into regex. Im kinda newbie in this topic – ampher911 Apr 16 '18 at 09:11
  • Reads the linked post. `(` and `)` are special. `(at)` matches `at` and puts the value into Group x. You need to escape `(` and `)`. – Wiktor Stribiżew Apr 16 '18 at 09:12

2 Answers2

2
var longString = "abc(at).com xyzat.com";
var regex = RegExp("[(]at[)]");
var wordList = longString.split(" ").filter((elem, index)=>{
    return regex.test(elem);
})

This way you will get all the word in an array that contain "at" in the provided string.

  • It catches all of the `at` in string. However not only (at) but every word contains "at" as well (e.g cat, what...ect). Do you know how can I catch only `(at)`? I tried with your code var `regex = RegExp("at");` but it doesn't work – ampher911 Apr 16 '18 at 09:04
  • yes its very simple check the updated code below;- var longString = "abc(at).com xyzat.com"; var regex = RegExp("[(]at[)]"); var wordList = longString.split(" ").filter((elem, index)=>{ return regex.test(elem); }) – Rajat Rastogi Apr 17 '18 at 05:31
0

You could use \S+ to match not a whitespace character one or more times and escape the \( and \):

\S+\(at\)\S+\.\w{2,}

The fourth bird
  • 154,723
  • 16
  • 55
  • 70