대부분의 경우 HTML과 RegEx가 함께 사용되지 않으며 HTML을 구문 분석 할 때 첫 번째 생각은 RegEx가 아니어야합니다.
그러나이 상황에서는 게시자 주간지에서 해당 페이지의 수행 방식이 변경 될 때까지 마크 업은 괜찮아 보이는 것처럼 보입니다. 여기
는 해당 라인을 잡아, 데이터를 추출으로 정렬하고 다시 넣어하는 함수입니다 :
($ j를 jQuery를하다)
function reorderPwList()
{
var Container = $j('#article span.table');
var TargetLines = /^.+?(\d+(?:,\d{3})*) copies\.<br ?\/?>$/gmi
var Lines = Container.html().match(TargetLines);
Lines.sort(sortPwCopies);
Container.html(Lines.join('\n'));
function sortPwCopies()
{
function getCopyNum()
{ return arguments[0].replace(TargetLines,'$1').replace(/\D/g,'') }
return getCopyNum(arguments[0]) - getCopyNum(arguments[1]);
}
}
그리고 거기에 사용 된 정규 표현식에 대한 설명 :
^ # start of line
.+? # lazy match one or more non-newline characters
( # start capture group $1
\d+ # match one or more digits (0-9)
(?: # non-capture group
,\d{3} # comma, then three digits
)* # end group, repeat zero or more times
) # end group $1
copies\. # literal text, with . escaped
<br ?\/?> # match a br tag, with optional space or slash just in case
$ # end of line
정규식 플래그 gmi
가 사용되는, g lobal, m ulti- 위해 -은 ('복사'전과 'BR'후 유효 사람 만의 공간 가독성을 위해, 나는 그룹을 들여 쓰기했습니다.) 회선 모드, 대소 문자 구분 i nsensitive matching.
< OLD 답변> 당신은 당신이 (DOM/jQuery를 사용하여)에서보고 싶은 텍스트 만 추출하면
, 당신은 다음에 전달할 수 있습니다 함수를 사용하여 관련 정보를 정렬 할 수있는 형식으로 변환합니다.
function makeSortable(Text)
{
// Mark sortable lines and put number before main content.
Text = Text.replace
(/^(.*)([\d,]+) copies\.<br \/>/gm
, "SORT ME$2 $1"
);
// Remove anything not marked for sorting.
Text = Text.replace(/^(?!SORT ME).*$/gm , '');
// Remove blank lines.
Text = Text.replace(/\n{2,}/g , '\n');
// Remove sort token.
Text = Text.replace(/SORT ME/g , '');
return Text;
}
그러면 숫자가 올바르게 정렬되도록 정렬 함수가 필요할 것입니다. 표준 JS array.sort 메서드는 텍스트를 정렬하고 20,000 앞에 100,000을 넣습니다.
/^(.*)([\d,]+) copies\.<br \/>/gm
/.../gm a regex with global-match and multi-line modes
^ matches start of line
(.*) capture to $1, any char (except newline), zero or more times
([\d,]+) capture to $2, any digit or comma, one or more times
copies literal text
\.<br \/> literal text, with . and/escaped (they would be special otherwise)
/^(?!SORT ME).*$/gm
/.../gm again, enable global and multi-line
^ match start of line
(?!SORT ME) a negative lookahead, fails the match if text 'SORT ME' is after it
.* any char (except newline), zero or more times
$ end of line
/\n{2,}/g
\n{2,} a newline character, two or more times
:
여기 아, 그리고 여기에 사용되는 정규 표현식에 대한 간단한 설명입니다/OLD 답변 0
<>
감사합니다. 정규식이 정렬되지 않는다는 것을 알고 있습니다. 그러나 일치하는 문자열을 가진 줄을 식별하고 그 줄 앞에 오는 숫자를 추출하는 것이 유용 할 거라고 생각했습니다.
데이터에 대한 내 설명이 매우 분명하다고 생각했습니다. 어떤 사건의 샘플도 있습니다 :
산타 클로스 Rachael Hale (Little, Brown/LB Kids, $ 6.99). 125,000 부.
수잔 콜린스 (Scholastic Press, $ 17.99)의 기아 게임. 200,000 부.
너무 많은 장난감 David Shannon (Scholastic/Blue Sky, $ 16.99). 150,000 부.
원하는 출력은 첫 번째 줄을 맨 아래로 이동시킵니다. – user189880
좋아요, 나는 당신이 정규식을 분류 할 수있을 거라 생각한 인상을 받았습니다. 나에게 데이터에 대한 설명이 명확하지 않은 경우 명확한 설명을 요구하지 않았다. 그러나 베드로가 이해 한 것 같습니다. –