brownie brownie - 5 months ago 8
Javascript Question

Remove Number with Regex

i want to remove number at beginning and the end of word in one sentence for example:


"123helo helo123"


then it will return


"helo helo"


I've tried this pattern:

/^[0-9]|[0-9]$/


but it just recognized them as one string but not in words. Can you help me?

Answer

To answer your question, including specifically where you mention "at beginning and the end of word", this should suffice

str.replace(/\b\d+|\d+\b/g, '')

\b is the word-boundary character. The above removes all numbers directly after or directly before a word-boundary.

Comments