2013-06-18 2 views
0

<0> (여기서 0은 실제로 모든 숫자가 될 수 있음) 문자열을 취하여 제거하고 실제 숫자 값을 가져 오는 Javascript 함수를 작성하려고합니다.Javascript 정규식과 일치하는 문자열의 실제 값을 어떻게 얻을 수 있습니까?

'By now you\'re probably familiar with X, a drawing application that has won an Awards and plenty of attention for its user interface, which has rethought the way basic interactions like pinch-to-zoom or color selection should work on a touchscreen.' 

이 배열 : 현재 내가이이

[0, 1, 2, 3] 

'By now you\'re probably familiar with <0>X, a drawing application that has won an <1>Awards and plenty of attention for its user interface, which has rethought the way basic interactions like <2>pinch-to-zoom or <3>color selection should work on a touchscreen.' 

나는 그것이 나를 다시이 문자열을주고 싶어 : 예를 들어,이 문장을 주어진다면 :

function(sentence) { 
    return sentence.split(/\<[0-9]+\>/).join(''); 
} 

이것은 분명히 단지 후퇴 그 문장은 없다. 태그 안에 숫자 값이 있어야합니다. 이 일을 할 수있는 방법이 있습니까?

+2

마법의 철자를 수정해야합니까? – Rudu

+0

'<0>'문자를 제거하고 그 번호가 제거 된 문장을 배열 * 및 *으로 되 돌리시겠습니까? –

+0

@Rudu 와일드 카드. Lol no – chromedude

답변

2

나는 좋을 것 :

function regexAndArray (str) { 
    var reg = /(<(\d+)>)/g, 
     results = { 
      string : '', 
      stripped : [] 
     }; 
    results.string = str.replace(reg, function(a,b,c){ 
     results.stripped.push(c); 
     return ''; 
    }); 
    return results; 
} 

console.log(regexAndArray('By now you\'re probably familiar with <0>X, a drawing application that has won an <1>Awards and plenty of attention for its user interface, which has rethought the way basic interactions like <2>pinch-to-zoom or <3>color selection should work on a touchscreen.')); 

JS Fiddle demo합니다.

참고 :

+0

굉장합니다. 감사! – chromedude

+0

정말 도움이되어 기쁘게 생각합니다. –

관련 문제