Mehrdad201
Mehrdad201

Reputation: 123

How can we understand that a div has a unicode character or not with JQUERY

I want to recognize all objects in my web page that contains at least one UNICODE character.

By this way, I want to perform a specific CSS class to those elements that has UNICODE characters (maybe they are completely UNICODE or maybe they are contain NON-UNICODE & UNICODE together. For example a div that has English and Arabian.)

Is there any way in order to do this via JQUERY in client side of browsers?

Please guide me.

Thanks a lot.

Upvotes: 1

Views: 1104

Answers (2)

bobince
bobince

Reputation: 536389

(All text in a web browser is Unicode, by definition. You probably mean to detect Unicode characters that are not also ASCII characters.)

You would have to walk over all elements and check their text with a regex, for example:

$('*').each(function() {
    if (/[\u0080-\uFFFF]/.test($(this).text()))
        $(this).addClass('hasnonascii');
});

this will be slow. It's also recursive, so eg you'll get a class="hasnonascii" on <body> if there's any non-ASCII content on the page.

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use XRegExp with the unicode plugin to write a regular expression to check for unicode characters within the text of certain elements.

Upvotes: 0

Related Questions