Skip to content
Advertisement

How to remove white space in between ascii and nonascii chars?

For example:

JavaScript

I want to find at least 3 ascii chars, followed by a space, then followed by a nonascii char, and replace the white space with empty string. My code has two issues:

  1. How to write the replacement string for (s)?

  2. How to make it also work for the reverse order of s2?:

    [^a-zA-Z0-9]

Advertisement

Answer

Put the strings that you want to keep in the result in capture groups, then reference them in the replacement.

JavaScript

You don’t need to use {3,}, you can just use {3}. This will copy the last 3 characters to the result. All the preceding characters will be copied by default because they’re not being replaced.

You can also do it with lookarounds, by matching a space that’s preceded by 3 ASCII characters and followed by a non-ASCII. Then you replace the space with an empty string.

JavaScript

You can use alternative in this method to match both orders

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement