简体   繁体   中英

How to get everything in between these two strings?

How can I get everything between these two strings?

ATemp.runFunct( <I WANT EVERYTHING IN BETWEEN THESE TWO PARENS> ), // comment

I want everything in between the parens above, with the outside strings as a constant.

I have tried multiple regex expressions, but they have not worked, such as:

ATemp\\.runFunct\\(*.?\\), \\/\\/ comment

Any ideas? Thanks! (PS, I'm using javascript)

Here is the FIDDLE: http://jsfiddle.net/mikea80/kSuk4/

var str = "(Hello world!)";
var startIndex = str.indexOf("(");
var endIndex = str.indexOf(")");
var res = str.substring(startIndex + 1, endIndex); 
alert(res);

*.? should be .*? . Otherwise you are matching zero or more ( . You probably also want to use a capture group so that you can actually reference that part later on: (.*?)

Example:

var match = /ATemp\.runFunct\((.*?)\), \/\/ comment/.exec('ATemp.runFunct( <I WANT EVERYTHING IN BETWEEN THESE TWO PARENS> ), // comment');
alert(match[1]);

.exec returns an array, where the first element is the complete match and the second element is the value of the first capture group (and so on).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM