LoginSignup
0
0

More than 1 year has passed since last update.

Javascriptで複数の文字を削除する

Posted at

正規表現を用いて、複数の文字を削除するやり方を学んだのでメモ書き。

想定問題:与えられた文字列から母音を削除して出力する。
Ex. ohayougozaimasu => hygzms

使ったもの

replace

コード

const greeting = "ohayougozaimasu";
const goodmorning = greeting.replace(/[aiueo]/gi, '');
console.log(goodmorning);
//hygzms

replaceメソッドについて

replaceメソッドは文字列を置換するために用いられる。 使い方は以下の通り。
文字列.replace(置換したい文字列, 置換する文字列)

引数には文字列または正規表現を指定する。
注意点としては、最初に見つかった指定文字列(文字)だけを置換する点。置換対象が2箇所以上ある場合は、最初のものだけが置換され、残りの箇所は置換されない。
もし、指定した文字列(文字)をすべて置換したい場合は、「gオプション」を指定する。gオプションを用いる場合の記述は、置換したい文字列を「/」でくくり、その後にgをつける。

文字列.replace(/置換したい文字列/g, 置換する文字列)

さらに、大文字と小文字を無視して置換したい場合は、「giオプション」を用いる。

文字列.replace(/置換したい文字列/gi, 置換する文字列)

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0