2

This script returns a value like 33 | 4 and I need only the the first whole number, in this case 33.

Can I use .replace( /[^\d].*/, '' ) and where/how to put this? Or is there better solutions?

You are all very helpful, but i'm in a stage where I need to see my full script or a line from it with you solutions implemented ;-)

Thanks

jQuery(document).ready(function($) { $("#input_1_1").blur(function() { // sets the value of #input_1_8 to // that which was entered in #input_1_1 $("#input_1_8").val($("#input_1_1").val()); }); }); 
3
  • 5
    Just call parseInt() on the value. It will read all leading digits until a non-digit character is found: jsfiddle.net/c27xfdoh Commented Mar 26, 2019 at 11:13
  • str.split(/\D+/)[0] Commented Mar 26, 2019 at 11:14
  • use "33 | 4".split('|').map(a=> trim(a))[0] Commented Mar 26, 2019 at 11:14

3 Answers 3

1

You can use parseInt() or split your string with | and get the first result

console.log(parseInt("33 | 4")) // or console.log("33 | 4".split(" | ")[0])

In your case you can do

jQuery(document).ready(function($) { $("#input_1_1").blur(function() { $("#input_1_8").val(parseInt($("#input_1_1").val())); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this,

var str = "33 | 4"; var res = str.split("|");//split with '|' it will return array console.log(res[0].trim());//33 

You can do trim on it or parseInt, whatever you want to get your task done.

Comments

-1

With split you can split a string with any character, my example:

<script> var stringexample="45.89 | 65"; alert(takefirst(sting)); } function takefirst(){ var a=string.split("|"); return a[0] } </script> 

Also you can use split for separete character for character like this

var a=stringexample.split(""); for(var i=0;i<a.lenght;i++){ alert(a[i]); } 

Alerts:

4 5 . 8 

Etc..

6 Comments

Firstly, please take the time to describe what the issues are and why this solution helps. Remember, we're here to educate people, not just dump code on them. Secondly this will return 3, not 33
Chill, i not was finished my answer and you post a comment let me one minute be chill be happy
I am very happy :) My point, which you seem to be missing, is that your answer does not return what the OP is asking for.
My bad so, sorry
No problem. If you edit the answer to correct it I'll gladly remove my downvote
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.