이런 종류의 일을 위해 정규 표현식을 이길 수있는 것은 없습니다. 그러나 유지 관리가 어렵고 (게시물에서 지적한 것) 매우 큰 문제로 인해 성능 문제가있는 두 가지 문제가 있습니다. 하나의 regexp가 처리 할 수있는 대안이 얼마나 많은지는 모르지만, 20-30까지는 아무 문제가 없을 것이라고 생각합니다.
따라서 배열이나 문자열 일 수있는 일부 데이터 구조에서 동적으로 정규 표현식을 만드는 코드가 필요합니다. 나는 유지하기가 가장 쉽기 때문에 개인적으로 찌름을 선호합니다. 당신이있어 일단
// taken from http://www.ranks.nl/resources/stopwords.html
stops = ""
+"a about above after again against all am an and any are aren't as "
+"at be because been before being below between both but by can't "
+"cannot could couldn't did didn't do does doesn't doing don't down "
+"during each few for from further had hadn't has hasn't have "
+"haven't having he he'd he'll he's her here here's hers herself "
+"him himself his how how's i i'd i'll i'm i've if in into is isn't "
+"it it's its itself let's me more most mustn't my myself no nor "
+"not of off on once only or other ought our ours ourselves out "
+"over own same shan't she she'd she'll she's should shouldn't so "
+"some such than that that's the their theirs them themselves then "
+"there there's these they they'd they'll they're they've this "
+"those through to too under until up very was wasn't we we'd we'll "
+"we're we've were weren't what what's when when's where where's "
+"which while who who's whom why why's with won't would wouldn't "
+"you you'd you'll you're you've your yours yourself yourselves "
// how many to replace at a time
reSize = 20
// build regexps
regexes = []
stops = stops.match(/\S+/g).sort(function(a, b) { return b.length - a.length })
for (var n = 0; n < stops.length; n += reSize)
regexes.push(new RegExp("\\b(" + stops.slice(n, n + reSize).join("|") + ")\\b", "gi"));
가, 나머지는 분명하다 :
regexes.forEach(function(r) {
text = text.replace(r, '')
})
당신은 정규식 길이와 정규 표현식에의 총 수 사이의 최적의 균형을 찾기 위해 reSize
값을 실험 할 필요가있다. 실적이 중요한 경우 생성 부분을 한 번 실행 한 다음 결과 (예 : 생성 된 정규식)를 어딘가에 캐시 할 수 있습니다.
관련 항목 : [John Resig 's Series by Dictionary Lookups] (http://ejohn.org/blog/revised-javascript-dictionary-search/) –
"스톱 워드"는 몇 개입니까? 대답은 중요 할 수 있습니다. – ChaosPandion
[문자열의 일반적인 단어를 필터링하는 JavaScript 코드] (http://stackoverflow.com/questions/6686718/javascript-code-to-filter-out-common-words-in-a-string)의 가능한 복제본입니다. 솔루션은 문자열에서 사전을 작성합니다. 사전으로 시작하면 해당 부분을 건너 뛸 수 있습니다. – outis