embedchainjs

Форк
0
26 строк · 1.1 Кб
1
/**
2
 * This function takes in a string and performs a series of text cleaning operations.
3
 * @param {str} text: The text to be cleaned. This is expected to be a string.
4
 * @returns {str}: The cleaned text after all the cleaning operations have been performed.
5
 */
6
export function cleanString(text: string): string {
7
  // Replacement of newline characters:
8
  let cleanedText = text.replace(/\n/g, ' ');
9

10
  // Stripping and reducing multiple spaces to single:
11
  cleanedText = cleanedText.trim().replace(/\s+/g, ' ');
12

13
  // Removing backslashes:
14
  cleanedText = cleanedText.replace(/\\/g, '');
15

16
  // Replacing hash characters:
17
  cleanedText = cleanedText.replace(/#/g, ' ');
18

19
  // Eliminating consecutive non-alphanumeric characters:
20
  // This regex identifies consecutive non-alphanumeric characters (i.e., not a word character [a-zA-Z0-9_] and not a whitespace) in the string
21
  // and replaces each group of such characters with a single occurrence of that character.
22
  // For example, "!!! hello !!!" would become "! hello !".
23
  cleanedText = cleanedText.replace(/([^\w\s])\1*/g, '$1');
24

25
  return cleanedText;
26
}
27

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.