Angular指令找到单词。找不到带有变音符号的单词

时间:2016-06-15 11:49:09

标签: angularjs regex angularjs-directive coffeescript

使用$ filter我得到了翻译,并且使用scope.keywords我得到了我想要找到的单词。它工作得很好,除了它找不到带有变音字符的单词,如:“cześć”。任何人都可以帮助我吗?

.directive 'highlight', ($filter) ->
    component = (scope, element, attrs) ->

        scope.highlightedText = $filter('translate')(scope.highlight)
        if !attrs.highlightClass
            attrs.highlightClass = 'angular-highlight'

        replacer = (match, item) ->
            console.log match
            '<span class="' + attrs.highlightClass + '">' + match + '</span>'

        tokenize = (keywords) ->
            keywords = keywords.replace(new RegExp(',$', 'g'), '').split(',')
            i = undefined
            l = keywords.length
            i = 0
            while i < l
                keywords[i] = '\\b' + keywords[i].replace(new RegExp('^ | $', 'g'), '') + '\\b'
                i++
            console.log keywords
            keywords

        scope.$watch 'keywords', ->
            if !scope.keywords or scope.keywords == ''
                element.html scope.highlightedText
                return false
            tokenized = tokenize(scope.keywords)
            regex = new RegExp(tokenized.join('|'), 'gmi')
            console.log tokenized
            console.log regex
            # Find the words
            html = scope.highlightedText.replace(regex, replacer)
            console.log html
            element.html html
            return
        return

    {
        link: component
        replace: false
        scope:
            highlight: '@'
            keywords: '@'
    }

1 个答案:

答案 0 :(得分:1)

keywords[i] = '\\b' + keywords[i].replace(new RegExp('^ | $', 'g'), '') + '\\b'

改为 keywords[i] = '\\b' + keywords[i].replace(new RegExp('^ | $', 'g'), '')