javascript regex find words contains (at) in text [duplicate] - javascript

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
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.

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.

You could use \S+ to match not a whitespace character one or more times and escape the \( and \):
\S+\(at\)\S+\.\w{2,}

Related

Replace a string containing special characters [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Trying to replace a string that contains special characters. The purpose of this is to convert the query string into an understandable format for end users.
full string is:
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
Specifically the portion after ^NQ, in this example: opened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday(). I have split the original string with indexOf(^NQ) and passing the resulting sub-strings to a function. I'm then trying a .replace() as below:
var today = replacementString.replace(/(ONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday())/g, ' is today ');
replacementString = today;
I have tried with various combinations of the above line, but have not returned what I am hoping for.
I've had no issues replacing special characters, or strings without special characters, but the combination of the 2 is confusing/frustrating me.
Any suggestions or guidance would be appreciated
You should escape the () to \(\) to match it literally or else it would mean a capturing group. For the match you could also omit the outer parenthesis and you have to escape the dot \. to match it literally.
ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
var today = str.replace(/ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)/g, ' is today ');
replacementString = today;
console.log(today);

Regex allows spaces [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
For the following regex expression:
var regex = new RegExp("^(www\\.)?[0-9A-Za-z-\\.#:%_\+~#=]+(\\.[a-zA-Z]{2,})+(/.*)?(\\?.*)?");
I don't understand why the string "www.goo gle.com" passes the regex test. When I did this:
var regex = new RegExp("^(www\\.)?[0-9A-Za-z-\\.#:%_\+~#=]+(\\.[a-zA-Z]{2,})+(/.*)?(\\?.*)?$");
i.e. adding $ in the end of the regex string prevents the above string passing, which is what I would want.
I tried finding a "simulator" online to help me figure out how the regex is matching but couldn't find much help.
www.goo gle.com passes the test since, www. is matched by [0-9A-Za-z-\\.#:%_\+~#=]+ and
goo is matched by (\.[a-zA-Z]{2,})+. In contrast, (www\\.)?, and the last two groups are optional, so the regex is satisfied even if they are not matched, hence there's no need to further match gle.com.
By adding $, the regex no longer matches, since the space is not matched by any of the subexpressions.

Regex \s\s matches `return + place` [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I have constructed a string as follows
let str = String.fromCharCode(13) + ' ';
Next, I would like to know if the string contains two spaces
str.match(/\s\s/)
The weird thing is that it is a match. But if I do
str.match(/ /)
it isn't. Can someone explain to me why this is happening ?
The '\s' pattern allows you to match any kind of whitespace, not just space itself. For a more detailed list you can check here for example.
For reference (copied from the developer reference):
Matches a single white space character, including space, tab, form
feed, line feed and other Unicode spaces. Equivalent to [
\f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
For example, /\s\w*/ matches " bar" in "foo bar".

JS Regex to match character outside nested braces [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need to write a regex to target commas that exist only outside a pair of brackets or braces.
Currently I have:
var regex = /,(?![^{]*})(?![^[]*])/g
When the target string is:
var str = '"a":[{"b":2,"c":["d"]}],"b":2' // OK: only second comma matches
the pattern correctly matches only the second comma.
When the target string is:
var str = '"a":[{"b":2,"c":{"d":9}}],"b":2' // OK: only second comma matches
the pattern also correctly matches only the second comma.
However, when the target string includes a array and object, the negative lookahead fails and the regex matches both commas.
var str = '"a":[{"b":2,"c":[{"d":9}]}],"b":2' // BAD: both commas match
This regex will work (see demo) with the mentioned example:
,(?!([^{]*{[^{]*})?[^{]*})(?!([^[]*\[[^[]*])?[^[]*])
But it's not a generic regex.
For each level of nested brackets you need to expand the regex. For example, to match also "a":[{"b":2,"c":[{"d":[{"e":9}]}]}],"b":2 you will need:
,(?!([^{]*{([^{]*{[^{]*})?[^{]*})?[^{]*})(?!([^[]*\[([^[]*\[[^[]*])?[^[]*])?[^[]*])
See second demo.
This is not a scalable solution, but it's the only one with regexes.
Just for curiosity.

Regex in Javascript using OR [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 6 years ago.
I'm trying to write a regex expression to match multiple characters such as , OR . OR : OR ( OR )
I have this
removePunctuation = /\.$|\,$|\:|\(|\)/;
word = rawList[j].replace(removePunctuation,"")
I just want to remove periods & commas at the end of the sentence but all instances of ( ) :
What am I doing wrong?
You need to add the "g" qualifier if you want to remove all the punctuation.
removePunctuation = /\.$|\,$|\:|\(|\)/g;
I'd go with something like this:
/[.,]+$|[:()]+/g
It'll match periods and commas at the end of a sentence, and brackets and colons everywhere.

Categories