简体   繁体   中英

Replace all occurrences of character except in the beginning of string (Regex)

I'm trying to get rid of all minuses/dashes in a string number, except the first occurrence. After fiddling with Regex (JavaScript) for half an hour, still no results. Does anyone know the fix?

Given:

-123-45-6

Expected:

-123456

Given:

789-1-0

Expected:

78910

This one will do as well(it means dashes not at the beginning of the string):

(?!^)-

Example:

text = "-123-45-6".replace(/(?!^)-/g, "");

一个简单的解决方案:

s = s.replace(/(.)-/g,'$1')

Jutr尝试:

'-123-45-6'.replace(/(\d)-/g, '$1');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM