javascript - Tell if string is pixels or percent then divide by two -


so have function allow user define custom width either pixels or percents.

the problem need half of number provide. here example of trying do:

user enters----------expected result

50%            25% 300px          150px 

any appreciated!

this works, although should validate input first.

parseint(str) / 2 + str.match(/(%|px)$/)[0] 

without validation, throws error if input doesn't end in % or px (can't null[0]). if instead want have missing or unrecognized units treated % default, this:

parseint(str) / 2 + (str.match(/(%|px)$/)||["%"])[0] 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -