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 8059067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:33:11+00:00 2026-06-05T09:33:11+00:00

Given an input string, generate an output string where all invalid sequences are either

  • 0

Given an input string, generate an output string where all invalid sequences are either removed or replaced with U+FFFD.

Is there a better method than implementing a state-machine char-by-char, or a non-native node.JS module available?

Invalid sequences are, for example, orphaned surrogates "\uD800", or other invalid multi-char sequences.

  • 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-06-05T09:33:13+00:00Added an answer on June 5, 2026 at 9:33 am

    The regex needed to match invalid sequences depends on what you want to include. To replace orphaned surrogates with U+FFFD, you can use something like this:

    var surrogates = /[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;
    str = str.replace(surrogates , function ($0) {
        return $0.length > 1 ? $0 : '\ufffd';
    });
    

    If you use the XRegExp library with its Unicode addons, you can use the \p{Cs} or \p{Surrogate} Unicode category instead of [\ud800-\udfff]. Using XRegExp will also give you easy access to other potentially relevant Unicode properties such as \p{Noncharacter_Code_Point}, \p{Co} or \p{Private_Use}, and \p{Cn} or \p{Unassigned}.

    Since you’re using Node.js, you can install XRegExp via npm using npm install xregexp. XRegExp’s npm module automatically includes the Unicode addons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate anagram output of a given string without the help
So given this input string: =?ISO-8859-1?Q?TEST=2C_This_Is_A_Test_of_Some_Encoding=AE?= And this function: private string DecodeSubject(string input) {
We all know that App Engine limits you to 1 MB for most input/output
How to generate an n-gram of a string like: String Input=This is my car.
This follows on from this question: Algorithm to generate spanning set Given this input:
I need to retrieve a regex pattern matched strings from the given input. Lets
Given the input: str = foo bar jim jam. jar jee joon. I need
Given an input sequence, what is the best way to find the longest (not
Given the input XML file: <acctInfo> <wfInfo> <aaa>1</aaa> <bbb>1</bbb> <ccc>1</ccc> <ddd>1</ddd> <eee>1</eee> </wfInfo> <acctInfo>
You are given as input an unsorted array of n distinct numbers, where n

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.