T
T
Treescapes2020-05-11 07:15:31
JavaScript
Treescapes, 2020-05-11 07:15:31

How to find and determine a number in a string?

Hello.

The definition of the number in Value does not work for me.
The data is read from the test.jem file, transferred to the lexems.js file, and the format is determined for the one in brackets.

( test.jem )

print(56); // Число
print(hello); // Чтобы проверялась ошибка
print("hello world"); // Строка
print( ); // Пустое значение


And this is where the string format is defined:
( lexems.js )
if (/print \((.*)\)/gim.test(copyString)) { // Находим что если скобочки без кавычек это число
    let num = copyString.replace(/print \((.*)\)/gim, '$1'); // Убираем принт и скобочки
    if ((typeof num) === 'string') { // Определение TypeError
        stringObject["TypeError"] = num;
        console.log(`TypeError: ${num} is not a Number. ${num} is String`);
    }
}


(Outputs that 56 (not a string) is a string. How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-05-11
@Treemscapes

num = Number(copyString.match(/\((.*)\)/)[1] || undefined)
if(!isNaN(num)) { console.log('это число!') }

copyString = 'print ("hello world"); //строка'
// undefined
copyString = 'print (56); '
// это число!

only you need to think separately, is print("56") a number or a string?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question