LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Sunday, January 28, 2018

正規表現で文章から半角英数字と記号を抜き出したい /[!-~]+/g


文字列の中から半角英数字と記号を取得したくて調べて書いたコードです

今回書いた半角英数字と記号だけを抜き出す正規表現

/[!-~]+/g



コード.gs
function get_halfwidth(){
  var str = "abcABC123!bcABC123?かなカナカナ仮名!ひらがなカタカナカタカナ漢字?⼀市ヶ谷代々木";
  var pattern = /[!-~]+/g;
  var result = str.match(pattern);
  Logger.log(result);
}
意訳
この機能がやること
探索対象の文字列を用意しておく
半角英数字と記号のパターン
一致するものを探して
ログに出す






参考

Unicode 10.0 Character Code Charts
http://www.unicode.org/charts/

Controls and Basic Latin
http://www.unicode.org/charts/PDF/U0000.pdf

Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...