0

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?

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
jill182
  • 91
  • 1
  • 3
  • 9

3 Answers3

2

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
0

Try /^\d*(.*)\d*$/ and match capture group 1. Look here to see how to use capture groups.

Community
  • 1
  • 1
Ken Clement
  • 748
  • 4
  • 13
  • Use `\d+` instead of `\d*` if you want to exclude the possibility that a number is omitted. – Ken Clement Jun 21 '16 at 03:40
  • This removes the digits at the beginning and end of the sentence (string), not each word. –  Jun 21 '16 at 04:33
-1

PHP solution:

$string = '123helo helo123';
$result = preg_replace('/^\d+|\d+$/', '', $string);
echo $result; // helo helo

Javascript solution:

var string = '123helo helo123';
var result = string.replace(/^\d+|\d+$/g, '');
console.log(result); // helo helo

Or this RegExp for triming each word in 123hello hello123 123hello

/\b\d+|\d+\b/
Sergey Khalitov
  • 987
  • 7
  • 17