开发者

Javascript - Search/React for each result

开发者 https://www.devze.com 2023-01-05 11:10 出处:网络
So if you have the following string: \"$(document).ready(function() {\" There are three open parentheses \"(\"

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

精彩评论

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

关注公众号