Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Insert Dash Between Numbers and Text Javascript [closed]

Tags:

javascript

Here are the example of input and output

Example input: SSS1111 Output: SSS-1111

Example input: SS111 Output: SS-111

like image 658
Marco Torrecampo Avatar asked Mar 02 '23 07:03

Marco Torrecampo


1 Answers

Assuming strings consist of one or more letters (A-Z) followed by one or more numbers (0-9):

var before = 'SSS1111'
var after = s.replace( /([a-z])(\d)/i, '$1-$2' )

The regexp matches matches any letter [a-z] followed by any digit \d and replaces it with the letter, followed by a dash, followed by the digit.

like image 69
kmoser Avatar answered Mar 07 '23 14:03

kmoser