开发者

Javascript - Search/React for each result

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+"("
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜