Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegExp Help!

Status
Not open for further replies.

jbaywatch

Programmer
Sep 5, 2001
8
US
I thought the /g was used to specify to keep searching for substrings after the first match?

I need to match all * + and - chars in a string.
(So I can parse the field names out and add them together).

This is my code, that doesn't work.

Code:
//this would eventually add the values of form fields 
//Text1 and Text2

var strSum = 'Text1+Text2' 

var oRE = new RegExp('(\+)|(\-)|(\*)|(k)/g');

var arr = oRE.exec(strSum);
alert('arr: ' + arr);
Thanks,
- J
 
would this
<script>
var strSum = 'Text1+Text2'
strSum=strSum.replace(/[+,-,*]*/g,'')
alert(strSum)
</script>
do yhe trick for you? Victor
 
jbaywatch

Would the String.split method not give you what you want?

Code:
var string = &quot;Text1+Text2&quot;;
var array = string.split( /[+-]/ );
for( var i = 0; i < array.length; i++ ) {
    alert( &quot;array[&quot; + i + &quot;] is &quot; + array[i] );
}

Cheers, NEIL :)
 
actually i meant (/[\+,\-,\*]*/g (w/o backslash - wasn't replaced) Victor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top