jsでnew RegExpは正規表現として扱えるようですが、//で囲まないでも正規表現と扱えるのですか?解説していただけると助かります。
js
1var fruits = ['apple', 'apricot', 'avocado', 'blueberry', 'cherry', 'coconut', 'cranberry', 'dragonfruit', 'durian', 'grape', 'grapefruit', 'guava', 'kiwi fruit', 'lemon', 'lime', 'lychee', 'mango', 'melon', 'watermelon', 'miracle fruit', 'orange', 'bloodorange','clementine','mandarine','tangerine','papaya','passionfruit','peach','pear','persimmon','physalis','plum/prune','pineapple','pomegranate','raspberry','rambutan','star fruit','strawberry']; 2 3$(function() { 4 var list = $("#list"); 5 6 function appendList(word) { 7 var item = $('<li class="list">').append(word); 8 list.append(item); 9 } 10 11 $("#submit").on("click", function() { 12 var input = $("#keyword").val(); 13 var reg = new RegExp("^" + input); 14 15 $(".list").remove(); 16 17 $.each(fruits, function(i, fruit) { 18 if (fruit.match(reg)) { 19 appendList(fruit); 20 } 21 }); 22 23 if ($(".list").length === 0) { 24 appendList("一致する果物はありませんでした"); 25 } 26 }); 27});
regの値は^inputとなっていると思うのですが、/^input/とする必要はないのですか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/11/01 09:32
2018/11/01 10:01
退会済みユーザー
2018/11/01 10:17
2018/11/03 07:59 編集