Re: Replacing string containing quotes

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance





Christian Perthen wrote:

I have a long string containing double and single quotes such as

"<a onclick="removeValue('Cuba')">Cuba</a>, <a
onclick="removeValue('Canada')">Canada</a>, <a class=a_small
onclick="removeValue('US')">US</a>"

How can I assigned it to a variable and then remove "<a
onclick="removeValue('Cuba')">Cuba</a>" part from it?


Here is an example that might do what you want or that you at could adapt to do what you want:

var string = "<a onclick=\"removeValue('Cuba')\">Cuba</a>, <a onclick=\"removeValue('Canada')\">Canada</a>, <a class=a_small onclick=\"removeValue('US')\">US</a>";

function removeOnclick (input, value) {
var onclickPattern = new RegExp('<a[^>]+onclick="removeValue\\(\'' + value + '\'\\)">' + value + '</a>', 'g');
return input.replace(onclickPattern, '');
}


alert(
removeOnclick(string, 'Cuba') + '\r\n\r\n' +
removeOnclick(string, 'Canada') + '\r\n\r\n' +
removeOnclick(string, 'US') + '\r\n\r\n'
);

So you need to use the RegExp constructor and pass in a string with the regular expression pattern as you obviously want to have a different expression depending on a function parameter (e.g. 'Cuba' or 'US').
And constructing regular expression patterns from strings is a tedious task as you need to take care of both string escape as well as regular expression escape mechanism so you end up with stuff like
'<a[^>]+onclick="removeValue\\(\''
above which needs to escape the single quote in the string with \' and needs to escape the backslash with \\ to ensure in the regular expression the parenthesis ( occurs escaped as \(.


--

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
.



Relevant Pages

  • Re: Unrecognized escape sequences in string literals
    ... If you don't know what your string literals are, ... Adding escape codes into the string literal doesn't change this ... extra effort required to defeat the compiler (forcing the programmer to ... And if you saw that in Python, you'd also know that there are some ...
    (comp.lang.python)
  • Re: Unrecognized escape sequences in string literals
    ... need worry that I've misinterpreted what a string literal means. ... You can't expect the compiler to save you from ... Adding escape codes into the string literal doesn't ... (This behavior is useful when debugging: if an escape sequence is ...
    (comp.lang.python)
  • Re: problems with opening files due to files path
    ... GUI or it is a console app. ... of what an escape character and escape sequence is. ... character) inside a string specially, it makes the character after the ...
    (comp.lang.python)
  • Re: Unrecognized escape sequences in string literals
    ... if he could just look at the string literal and know. ... friend is a programmer. ... If you don't know what a backslash escape is going to do, ... That's an enormous difference from Python, ...
    (comp.lang.python)
  • Re: Parsing a string into an array
    ... pragmas to let perl help you find mistakes. ... split Splits a string into a list of strings and returns ... The first argument to splitis a regular expression pattern. ...
    (perl.beginners)