Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to extract extract the weight number without g using javascript

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<p>What i have</p>
<input value="1475g - 1500g" /><br>
<p>what do i need.</p>

<input style="width:50px;" value="1475" /><span>g - </span><input style="width:50px;" value="1500" /><span>g</span>


</body>
</html>

Description

  1. From the above code in first text box i have been setting value of ‘1475g – 1500g’ but i need to do is show only the weight numbers in two different input box

for example

  1. string val = 1475g – 1500g;
  2. string Separated_value1=1475;
  3. string Separated_value2=1500;

>Solution :

Use split and replace.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const input = document.querySelector("input").value; // add an ID or class or something, this is just for demo purposes

const [ seperated1, seperated2 ] = input.replace(/g/g, "").split(" - "); // First regular expression removes all "g"s from the string. Split turns the string into an array using " - " as a delimiter

console.log(seperated1, seperated2);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading