Hi i want to use regular expression to check if a string is in the format :
1+2 or +12 or 12+ or 1-2 or -12 or 12-. CAn someone help?

What have you tried so far?

var num=(string.slice(0,2))
    if (/^\d+$/.test(num))
        document.write ("true");

That will check for one or more digits. What is the requirement for your examples?

i want to check if there is a number in 1st and 2nd position and an operator in the third. That is 32+. then it returns true

You can do:

/[-+]\d{2}|\d[-+]\d|\d{2}[-+]/

I am writing it like this n it's not working! why this?

var x=/[-+]\d{2}|\d[-+]\d|\d{2}[-+]/;

    if (string.test(x))
    {
        document.write("true");

    }

Perhaps a syntax difference. What's the value of string?

I am writing it like this n it's not working! why this?

var reg=/[-+]\d{2}|\d[-+]\d|\d{2}[-+]/;

    if (string.test(reg))
    {
        document.write("true");

    }

the value is +12
its not working!

var subject = '+12';
if (subject.match(/[\-+]\d{2}|\d[\-+]\d|\d{2}[\-+]\/[\-+]\d{2}|\d[\-+]\d|\d{2}[\-+]/)) {
    alert('Successful match');
} 
else {
    alert('Match attempt failed');
}

1+2=3
regular expresion /\d[-+]\d\=\d/ ,Is the regular expression good for the above statement??

Basically yes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.