ハイフンぽい文字列をUnicode に変換してそれを「全角の長音」に変換する

文字列をUnicode に変換して置換する

ここではJavascriptでハイフンぽい文字列をUnicode に変換してそれを「全角の長音」に変換する例を示します。

Unicode ⇔ 文字列 への相互変換

“\uXXXX"形式の4桁の16進数を文字に変換

String.fromCharCode(0xff0d); // "-"

文字を"\uXXXX"形式の4桁の16進数で表すに変換

"-".charCodeAt(0).toString(16); // "ff0d"

正規表現で置換する処理に応用

var h = v.replace( /[‐-−―]/g, 'ー');

は以下のように表すことができます。

v.replace(/[\u2010\uff0d\u2212\u2015]/g, '\u30fc');

検証

"‐-−―".replace(/[\u2010\uff0d\u2212\u2015]/g, '\u30fc');

参考

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Special_characters_in_regular_expressions https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode