Extract a first number from text

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
garzew2
Posts: 26
Joined: Sat Aug 28, 2010 8:54 pm

Extract a first number from text

Post by garzew2 » Sat Aug 04, 2012 10:22 am

Hello. I am trying to extract a number from text using firefox javascript.
My js file:

Code: Select all

ret = iimPlay("ext.iim")

extr =iimGetLastExtract(1)
alert (extr);

varr = extr.replace(/^[^\d]*(\d+)/ , '');
alert (varr);
ext.iim

Code: Select all

FRAME F=1
TAG POS=1 TYPE=FONT ATTR=TXT:Witaj<SP>urzytkowniku.<SP>Jesteś<SP>* EXTRACT=TXT
So ext.iim extract a text like this: Witaj urzytkowniku. Jesteś 23 w kolejce. Poczekaj 33 sekundy.
Numbers can be random, and i need to get only first number- in this example 23.
My javascriptfile (i created form information founded in internet) is using regexp to extract number, but it return all numbers - 2333.

I am not a programer and asking someone to help me fix this to get from extracted text only first number.
This first number can be a one, two or three digits number.
siniy
Posts: 118
Joined: Sat Nov 07, 2009 7:44 pm

Re: Extract a first number from text

Post by siniy » Sat Aug 04, 2012 8:57 pm

In my opinion "match" will be better here.

Code: Select all

var MyStr = "Test 11 its 22 imacros 33 script";
var num = MyStr.match(/\d+/);
if(num!==null)
	{
		alert(num[0]);
	}
	else
	{
		alert("No numbers found");
	}
Post Reply