0

I'm trying to find a regex to remove all leading/trailing spaces as well as all leading/trailing special characters.

If I have the string:

test = '~!#@$@  hello this is a #! test  ^^#!^^    '

I'd like it to return as:

'hello this is a #! test'

So special characters and spaces in between the first and last letter are preserved, but all leading/trailing ones are cut out. Right now I have this:

test.replace(/[^a-zA-Z ]/g,"").replace(/^\s+|\s+$/g, "")

which returns:

'hello this is a  test'

so it is removing all special characters and leading/trailing spaces. How can I preserve that "#!" between "a" and "test"?

2

1 Answer 1

3

By using the unicode flag u you can use the Unicode Categories for Letters \p{L} and Numbers \p{N} in a negative character class [^...].
And match them at the start ^ or | at the end $ to get anything that's not a letter or number and is trailing or leading.

Pattern:

^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$

Test Snippet:

let test = '~!#@$@  hello this is a #! test  ^^#!^^    ';

test = test.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');

document.write('['+test+']');

Sign up to request clarification or add additional context in comments.

2 Comments

This does the trick, I appreciate the explanation of what everything means!
@bmorgs Nice. Just beware that the unicode categories were only introduced with ES2018. So some browsers might not be able to use them. The regex /^[\W_]+|[\W_]+$/g from Wiktor will work, but it'll only keep ascii letters and digits [A-Za-z0-9]. Not letters like ę ã ö

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.