Babban matsala tare da bincika idan kirtani kawai ya ƙunshi lambobin lambobi shine cewa babu ƙayyadadden ƙayyadaddun ƙayyadaddun ƙayyadaddun yadda ake yin wannan. Wannan zai iya haifar da aiwatarwa daban-daban suna dawo da sakamako daban-daban, wanda zai iya zama da wuya a fahimta da kuma cirewa.
I want to check if a string only contains integer digits numbers. For example: <code>var str = "123"; // return true; var str = "123a"; // return false; </code> A: You can use <code>/^d+$/.test(str)</code>. This will test whether the string consists of one or more digits. If you want to allow for a leading minus sign, then use <code>/^-?d+$/.test(str)</code>. If you want to allow for an optional decimal point and fractional part, then use <code>/^-?d+(.d+)?$/.test(str)</code>. If you want to allow for an optional exponent, then use <code>/^-?(d+(.d*)?|.d+)([eE][-+]?d+)?$/.test(str)</code>. The last two expressions are the ones used by the built-in function <code>isFinite()</code>, which is what you should be using if your goal is to test whether a string can be converted into a number. (If your goal is something else, please edit your question.)
Yanayi
Sharuɗɗa kayan aiki ne mai ƙarfi a cikin JavaScript. Suna ba ku damar sarrafa kwararar lambar ku bisa wasu sharuɗɗan da ake cikawa.
Ɗayan amfani gama gari don sharuɗɗa shine bincika idan mai canzawa yayi daidai da takamaiman ƙima. Misali, kuna iya nuna saƙon kuskure idan shigarwar mai amfani ba ta da inganci.
Kuna iya amfani da bayanin idan bayanin don gwada ko yanayin gaskiya ne ko na ƙarya. Misalin lamba mai zuwa yana bincika don ganin ko shigar da mai amfani yana tsakanin 1 da 10:
idan (userInput <= 10) {// Nuna saƙon kuskure } in ba haka ba {// Nuna amsa ta al'ada } Hakanan zaka iya amfani da bayanin canzawa don gwada yanayi da yawa a lokaci ɗaya. Misalin lamba mai zuwa yana bincika ko shigarwar mai amfani yana tsakanin 1 zuwa 10, yana da tsawon aƙalla haruffa 3, kuma yana farawa da harafi: switch (userInput) { case"1": case"2": case"3": harka "a": harka "b": harka "c": karya; tsoho: // Nuna saƙon kuskure }
Idan kuma
Idan mai aiki ne na sharadi a JavaScript. Yana ba ku damar zaɓar tsakanin sakamako biyu masu yiwuwa. Sakamako na farko shine yanayin, kuma sakamako na biyu shine sakamakon idan sanarwa.
Idan kana so ka bincika ko lamba ma ko ba ta da kyau, za ka iya amfani da lambar mai zuwa:
idan (lamba % 2 == 0) {//ko da } wani {//m }
Sauran juzu'in zai aiwatar idan lambar ba ta ma.