반응형

만약 어떤 input 에 값이

값1, 값2,값3, 값4 , 값5...

이런식으로 컴마 사이의 공백이 중구난방 되어 있을 때

공백을 일정하게 만들어주는 함수입니다.

정규식을 사용하여 처리하는 방법입니다.

 

공백을 양쪽에 넣어주시고 싶으시다면

1
2
3
$("#test").val(function(i, v) {
    return v.replace(/\s*,\s*/g, " , ");
});
cs

이렇게 공백을 없애고 싶으시다면

1
2
3
$("#test").val(function(i, v) {
    return v.replace(/\s*,\s*/g, ",");
});
cs

이렇게 처리하시면 됩니다!

 

그리고 추가로 만약 맨 끝에 컴마만 제거하고 싶으시면

1
2
3
4
$("#test").val(function(i, v) {
    return v.replace(/,\s*$/"");
});
 
cs

이렇게 정규식을 이용하여 처리하시면 됩니다.

 

 

출처

https://stackoverflow.com/questions/33607564/jquery-add-space-after-and-before-comma

 

jQuery - Add space after and before comma

I trying to add space before and after comma in input/text on blur. this code works perfectly but I got a little issue with this, that I want to do this once, if you blur input, it going to add space

stackoverflow.com

 

반응형

+ Recent posts