0

I have large string with repeated some small string in JavaScript. I want to remove only that particular string but symbols should not remove or changed.

The large string is -

  var temp="day=1-CT=18-box=calender1_day=2-CT=19-CT=20-box=calender2_day=3-CT=30-box=calender3";

IN above string three sub string is joined with underscore(_). so I want to remove 'box=calender(n)' from each from these three sub string. Please help. Thanks in Advanced.

1
  • what should output look like? Commented Nov 29, 2013 at 5:02

5 Answers 5

1

Use simple string replacement function. Wherever box=calender(n) is found, it will be deleted.

temp = temp.replace('box=calender(n)','');
Sign up to request clarification or add additional context in comments.

1 Comment

'box=calender' is same but '(n)' will change in each string.. see the actual above string .
0

Try like this :

<script>
function myFunction()
{
  var temp="day=1-CT=18-box=calender1_day=2-CT=19-box=calender2_day=2-CT=20-box=calender2_day=3-CT=30-box=calender3";
    str = temp.replace(/box=calender\d/g, '');
    alert(str);
}
myFunction();
</script>

Comments

0

may be here is your clue for your search

$temp="day=1-CT=18-box=calender1_day=2-CT=19-CT=20-box=calender2_day=3-CT=30-box=calender3"; $patterns = array ('/box=calender[0-9]/',); $replace = array (''); echo preg_replace($patterns, $replace, $temp);

output

day=1-CT=18-_day=2-CT=19-CT=20-_day=3-CT=30-

Comments

0

Try:

var temp="day=1-CT=18-box=calender1_day=2-CT=19-box=calender2_day=2-CT=20-box=calender2_day=3-CT=30-box=calender3";
temp = temp.replace(/box=calender./g,"");

DEMO here.

Comments

0

Can you try this,

    var temp="day=1-CT=18-box=calender1_day=2-CT=19-box=calender2_day=2-CT=20-box=calender2_day=3-CT=30-box=calender3";
    var pt = /box=calender\d/g;
    temp = temp.replace(pt, "");

1 Comment

'box=calender' is same but '(n)' will change in each string.. see the actual above string .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.