1

I am trying to replace the javascript code in html by javascript

So I am testing like this.

var test = new String("<script type=\"text/javascript\"> test </script>");

test.replace(/<script type=\"text\/javascript\">.+<\/script>/g,"");
//this doesn't match

console.log(test.toString());

test.replace doesn't match and console.log shows this.(nothing happens!)

<script type="text/javascript"> test </script>

Is there any mistake??

whitebear
  • 11,200
  • 24
  • 114
  • 237
  • 5
    `.replace` **returns** a new string. But the more important question is, why are doing this? What problem are you trying to solve here? – Felix Kling Feb 16 '14 at 22:46
  • 1
    An aside not an answer, but you don't need `new String()` or `.toString()` there - every string in JS is an object of type string. – IMSoP Feb 16 '14 at 22:46
  • 1
    I'd be curious to know why you need to do this. – cookie monster Feb 16 '14 at 22:47
  • 1
    @IMSoP: Well, not quite. Most are string primitives. Using `new String` is a terribly bad idea. Anyways, whitebear: why’re you trying to do this? It doesn’t seem like a good idea somehow. – Ry- Feb 16 '14 at 22:47
  • 2
    @IMSoP: Technically no. There are string *primitives* and string *objects* and they behave slightly differently. You should never use `new String` thought, because it makes things very confusing. – Felix Kling Feb 16 '14 at 22:47
  • Also using inside Javascript will end the Javascript... Use something like "" to avoid the HTML parser seeing (even though it's inside quotes) – pscs Feb 16 '14 at 22:47
  • Your approach is not a good one, regular expressions should not be used for matching markup (since HTML is not a regular language). – RobG Feb 16 '14 at 22:47
  • 1
    @Felis Kling thanks.... I am ashmed... :-( – whitebear Feb 16 '14 at 22:47
  • @pscs: It could be external JavaScript. – Ry- Feb 16 '14 at 22:47
  • I made you confused sorry. honestly I am not using javascript but titanium. it uses javascript but not on browser. – whitebear Feb 16 '14 at 22:48
  • @whitebear - **Never** use primitive constructor in JavaScript. They will return incorrect values, for example, while `false` is false, `new Boolean(false)` is true. – Derek 朕會功夫 Feb 16 '14 at 22:48
  • @pscs—that does absolutely nothing of any value. A closing tag is denoted by ``, the rest is just for convenience. To property quote a closing script tag, use `<\/script>`. – RobG Feb 16 '14 at 22:49
  • @minitech and FelixKling: I stand corrected; I will have to read up on that distinction some time. JS is so much more intricate than it seems on the surface! – IMSoP Feb 16 '14 at 22:49
  • The constructor functions for primitives can be useful without `new`. But I guess then they're not technically a constructor. – cookie monster Feb 16 '14 at 22:51

1 Answers1

2

test.replace returns the replaced string, it does not replace test in place.

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

spiehr
  • 411
  • 2
  • 10