Posted on 2009/02/20 14:14 | CodeIN/Web



The JavaScript function for String Replace replaces the first occurrence in the string. The function is similar to the php function str_replace and takes two simple parameters. The first parameter is the pattern to find and the second parameter is the string to replace the pattern with when found. The javascript function does not Replace All...
str = str.replace(”find”,”replace”)

To ReplaceAll you have to do it a little differently. To replace all occurrences in the string, use the g modifier like this:
str = str.replace(/find/g,”replace”)

 
트랙백 주소 :: http://www.voiceportal.co.kr/532/trackback/
profile image

To be able to do what you want is the freedom, to enjoy what you do is the happiness.

  1. 황상범 2009/04/10 14:07 답글수정삭제

    잘 썼습니다.

    그런데, 저의 경우는 버그가 있네요. 수정해 봅니다.

    str = str.replace(/find/g,”replace”)

    위에서 찾는 문제에 각괄호를 해 줍니다.

    alert("|222222|33333|24444|5555".replace(/[|]/g,"<br>"))

    아마도 'or'로 읽혀지는 "|" 기호를 사용해서 그런 것이 아닌가 생각해 봅니다.

    잘 쓰겠습니다.

  2. kwangho 2009/07/08 19:46 수정삭제

    관리자의 승인을 기다리고 있는 댓글입니다

옵션
댓글 달기

블로그 이미지
Blog Image
김태정, All on Voiceportal

Voice portal에 대한 정보 공유.

by 김태정
프로필 버튼
프로필 상세보기
블로그롤 정보

Calendar*
«   2010/03   »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Counter*
Total
556032
Today
77
Yesterday
1320