Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  24] [ 3]  / answers: 1 / hits: 6923  / 4 Years ago, thu, may 14, 2020, 12:00:00

I need to remove some characters from a string. However, there are certain restrictions.



Example phrase:



There are two '@planes' which are almost ' identical in @ how they are built.



I need to find the pattern '@ and the next following '. Those characters need to be removed from the String. However, if there is only a ' or only a @ they should remain untouched. To clarify, the string can contain one or more words that are enclosed by '@ at the beginning and a single ' at the end. The enclosing characters need to be removed.



After removing the characters it should look like this:



There are two planes which are almost ' identical in @ how they are built.



Another example phrase:



A '@computer' can sometimes be very '@expensive in price' but in other cases it can be @cheap or even free'.



After removing the characters, it should look like this:



A computer can sometimes be very expensive in price but in other cases it can be @cheap or even free'.



The regex should work in JavaScript.


More From » regex

 Answers
7

Would /'@([a-z0-9s]+)'/gi work for you?



A '@computer' can sometimes be very '@expensive in price' but in other cases it can be @cheap or even free'..replace(/'@([a-z0-9s]+)'/gi, '$1');


You may check out https://regex101.com/ to get explanations in detail about the regexp itself.


[#3820] Tuesday, May 12, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nathanaelzechariahl

Total Points: 745
Total Questions: 86
Total Answers: 103

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
nathanaelzechariahl questions
Sat, Feb 27, 21, 00:00, 3 Years ago
Thu, Aug 20, 20, 00:00, 4 Years ago
Fri, Nov 22, 19, 00:00, 5 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Wed, May 15, 19, 00:00, 5 Years ago
;