So if you have the following string: "$(document).ready(function() {"
There are three open parentheses "("
I know there is the good 'ol string.replace(/(/g, "repl开发者_如何学Goacement_string"); way of doing things, but lets just say that doesn't exist for this question.
Now lets say I have a function that does replaces "(" with "?". Is there a way to perform the function once for every "(" in the string?
you could split the string using
var theStringinQuestion="$(document).ready(function() {";
var strArr=theStringinQuestion.split("(");
and then run the resultant array through a for in
loop like so:
var resultStr="";
for(substr in strArr){
if(someCondition){ // where someCondition is your condition
resultStr+=substr+"?";
}else{
resultStr+=substr+"("
}
}
精彩评论