Skip to content

clearStr

文字列から前後の空白、URL エンコード、引用符を取り除きます。

API

clearStr

戻り値

引数説明
string整えたあとの文字列string

パラメーター

パラメーター説明既定値
str整える文字列string必須
options設定のオプションClearStrOption{}

オプション

パラメーター説明既定値
urlencodedURL デコードを行うかどうかbooleantrue

使用例

基本的な使い方

js
import { clearStr } from 'ranuts';

const str = '  "hello world"  ';
const cleaned = clearStr(str);
console.log(cleaned); // 'hello world'

URL エンコードされた文字列

js
import { clearStr } from 'ranuts';

const encoded = '  "hello%20world"  ';
const cleaned = clearStr(encoded);
console.log(cleaned); // 'hello world'(自動でデコードされます)

URL デコードを止める

js
import { clearStr } from 'ranuts';

const str = '  "hello%20world"  ';
const cleaned = clearStr(str, { urlencoded: false });
console.log(cleaned); // 'hello%20world'(デコードされません)

引用符の扱い

js
import { clearStr } from 'ranuts';

const str1 = "'test'";
const str2 = '"test"';
console.log(clearStr(str1)); // 'test'
console.log(clearStr(str2)); // 'test'

補足

  1. 取り除くもの:前後の空白、シングルクォート、ダブルクォートを取り除きます。
  2. URL デコード:既定では URL デコードを行い、urlencoded: false で止められます。
  3. 使いどころ:ユーザーの入力や、URL のパラメータから取り出した値を整えるのによく使われます。

MIT ライセンスのもとで公開されています。