I have a regex which matches a string of length between 10 - 15, lower case alphabetic characters like so...

str.replace(/[a-z]{10,15}/g,"replacement word")); 

^^ The next step is I'd like to ignore any whitespace whilst matching.

I've tried

str.replace(/[a-z]{10,15}\s*/g,"replacement word")); 


str.replace(/[a-z]{10,15}\s?/g,"replacement word")); 

But it still doesn't ignore whitespace when matching. Even this: "abcdef ghijkl" should be matched because the space is needed to be ignored.

Can anyone experienced with js regex give a hand.

Recommended Answers

All 2 Replies

The regex you show checks for spaces after the word match. Is it an option to first remove all spaces from your string? Perhaps if you show some background or samples, that we can help you better.

Is this what you're trying to do?

str.replace(/[a-z\ ]{10,15}/g,"replacement word");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.