Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3879898
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:46:51+00:00 2026-05-19T22:46:51+00:00

Is it possible in Javascript to detect if a string contains multibyte characters? If

  • 0

Is it possible in Javascript to detect if a string contains multibyte characters? If so, is it possible to tell which ones?

The problem I’m running into is this (apologies if the Unicode char doesn’t show up right for you)

s = "";

alert(s.length);    // '2'
alert(s.charAt(0)); // '��'
alert(s.charAt(1)); // '��'

Edit for a bit of clarity here (I hope). As I understand it now, all strings in Javascript are represented as a series of UTF-16 code points, which means that regular characters actually take up 2 bytes (16 bits), so my usage of “multibyte” in the title was a bit off. Some characters do not fall in the Basic Multilingual Plane (BMP), such as the string in the example above, and so they take up two code points (32 bits). That is the question I was asking. I’m also not editing the original title, since to someone who doesn’t know much about this stuff (and hence would be searching SO for info about it), “multibyte” would make sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:46:51+00:00Added an answer on May 19, 2026 at 10:46 pm

    JavaScript strings are UCS-2 encoded but can represent Unicode code points outside the Basic Multilingual Pane (U+0000 – U+D7FF and U+E000 – U+FFFF) using two 16 bit numbers (a UTF-16 surrogate pair), the first of which must be in the range U+D800 – U+DFFF.

    Based on this, it’s easy to detect whether a string contains any characters that lie outside the Basic Multilingual Plane (which is what I think you’re asking: you want to be able to identify whether a string contains any characters that lie outside the range of code points that JavaScript represents as a single character):

    function containsSurrogatePair(str) {
        return /[\uD800-\uDFFF]/.test(str);
    }
    
    alert( containsSurrogatePair("foo") ); // false
    alert( containsSurrogatePair("f") ); // true
    

    Working out precisely which code points are contained in your string is a little harder and requires a UTF-16 decoder. The following will convert a string into an array of Unicode code points:

    var getStringCodePoints = (function() {
        function surrogatePairToCodePoint(charCode1, charCode2) {
            return ((charCode1 & 0x3FF) << 10) + (charCode2 & 0x3FF) + 0x10000;
        }
    
        // Read string in character by character and create an array of code points
        return function(str) {
            var codePoints = [], i = 0, charCode;
            while (i < str.length) {
                charCode = str.charCodeAt(i);
                if ((charCode & 0xF800) == 0xD800) {
                    codePoints.push(surrogatePairToCodePoint(charCode, str.charCodeAt(++i)));
                } else {
                    codePoints.push(charCode);
                }
                ++i;
            }
            return codePoints;
        }
    })();
    
    alert( getStringCodePoints("f").join(",") ); // 102,119558
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Is it possible to detect binary data in JavaScript? I'd like to be able
Possible Duplicate: How to detect if JavaScript is disabled? Is it possible to detect
Possible Duplicate: How to detect if JavaScript is disabled? How to find whether javascript
Is it possible using jQuery (or any other JavaScript function) to detect when an
Possible Duplicate: How to detect if JavaScript is disabled? My first web application relies
Is it possible to use JavaScript to detect whether a .swf file has loaded
Is it possible to use JavaScript to detect whether Google Chrome Frame is using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.