How would one make the ECMA-262 String.replace method work with a
string literal?
For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.
Please note my square brackets are not regular expression syntax.
Thanks,
Gary 21 3409
gary wrote: How would one make the ECMA-262 String.replace method work with a string literal?
For example, if my string was "HELLO[WORLD]" how would I make it work in this instance.
Please note my square brackets are not regular expression syntax.
Try
alert("hello".r eplace(/ll/i, 'jj') );
String literals are objects in JavaScript, unlike C.
--
Ian Collins.
Thank you very much for the reply, however, this does not solve my
problem.
For example
var h = "he[ll]o";
var regx = new RegExp ( h , 'i' );
alert(h.replace (regx, 'jj') );
Displays "he[ll]o" and not "he[jj]o" which is what I want.
Gary
Ian Collins wrote: gary wrote: How would one make the ECMA-262 String.replace method work with a string literal?
For example, if my string was "HELLO[WORLD]" how would I make it work in this instance.
Please note my square brackets are not regular expression syntax. Try
alert("hello".r eplace(/ll/i, 'jj') );
String literals are objects in JavaScript, unlike C.
-- Ian Collins.
gary wrote:
Please don't top post! Ian Collins wrote:
gary wrote:
How would one make the ECMA-262 String.replace method work with a string literal?
For example, if my string was "HELLO[WORLD]" how would I make it work in this instance.
Please note my square brackets are not regular expression syntax.
Try
alert("hello" .replace(/ll/i, 'jj') );
String literals are objects in JavaScript, unlike C. Thank you very much for the reply, however, this does not solve my problem.
For example
var h = "he[ll]o"; var regx = new RegExp ( h , 'i' ); alert(h.replace (regx, 'jj') );
Displays "he[ll]o" and not "he[jj]o" which is what I want.
Which is exactly what my example does. What are you attempting to do?
--
Ian Collins.
Ian Collins wrote: gary wrote: How would one make the ECMA-262 String.replace method work with a string literal?
<snip> Try
alert("hello".r eplace(/ll/i, 'jj') );
String literals are objects in JavaScript, unlike C.
No they are not. String primitive and string objects are distinct in
javascript, and string literals define string primitives.
The above works because the dot operator implicitly type-converts its
left hand side operand into an object, so the above is implicitly
equivalent to:-
alert((new String("hello") ).replace(/ll/i, 'jj') );
Richard.
Richard Cornford wrote: Ian Collins wrote: alert("hello".r eplace(/ll/i, 'jj') );
String literals are objects in JavaScript, unlike C.
No they are not. String primitive and string objects are distinct in javascript, and string literals define string primitives.
The above works because the dot operator implicitly type-converts its left hand side operand into an object, so the above is implicitly equivalent to:-
alert((new String("hello") ).replace(/ll/i, 'jj') );
But perhaps you could clear something up for me. If I pass a string
object to a function (that may return an altered string), a reference
should be passed. So it would be more efficient, in general, to pass a
string object and have the function change the underlying string rather
than having to return the string, since returning means that the string
must be copied when there are no changes. My question is, how does one
change the string of the string object without bothering the reference?
I could always wrap the string in an array, but that doesn't seem like
it's in the right spirit.
Csaba Gabor from Vienna
In this non working example, I'd like the line in the function to
change the contents of the string object instead of changing the object
itself (which, as a result, won't be reflected in the caller).
var oStr = new String("example string")
function changeoStr (oStr) {
// possibly oStr.__parent__ or oStr.__proto__ could help?
// doesn't work. I'd just like the underlying string changed
oStr = "new string";
}
changeoStr(oStr );
alert (oStr); // I'd like the alert to show "new string"
Csaba Gabor wrote: Richard Cornford wrote:
<snip> ... . String primitive and string objects are distinct in javascript, and string literals define string primitives.
<snip> ... . My question is, how does one change the string of the string object without bothering the reference?
<snip>
Javascript does not have any means of changing the internal value of a
String object.
Richard.
Richard Cornford wrote: Ian Collins wrote:
gary wrote:
How would one make the ECMA-262 String.replace method work with a string literal?
<snip>
Try
alert("hello" .replace(/ll/i, 'jj') );
String literals are objects in JavaScript, unlike C.
No they are not. String primitive and string objects are distinct in javascript, and string literals define string primitives.
OK, thanks for pointing that out.
--
Ian Collins.
Richard Cornford wrote: Csaba Gabor wrote: Richard Cornford wrote: <snip> ... . String primitive and string objects are distinct in javascript, and string literals define string primitives.
<snip> ... . My question is, how does one change the string of the string object without bothering the reference? <snip>
Javascript does not have any means of changing the internal value of a String object.
Thanks, I was afraid you might say something like that.
Well, if I can't unwrap the string object to get to the string, at
least I can wrap up the string in my own object to pretend that it's a
string object. Thus, I have a cleaner way to pass the string by
reference than wrapping it in an array. That is to say, other than a
distinct way to create it and a new in place replacement function
(.selfReplace), it appears to old code as a normal string.
I tried to modify the String object itself, similarly to the below (by
giving it a self value, to override any original value), but I didn't
get it working (yet?).
Csaba
function oString(str) {
this.__proto__ = new String(str);
this.toString = function() {return this.__proto__. toString(); };
this.valueOf = function() {return this.__proto__. valueOf(); };
this.selfReplac e = function(needle RegExp, strReplacement) {
this.__proto__ = new String(this.rep lace(needleRegE xp,
strReplacement) ); } }
function changeStr (oStr, newStr) { oStr.selfReplac e(/.*/, newStr); }
var oStr = new oString("my string"); alert("length: " + oStr.length);
oStr.selfReplac e (/str/,"th"); alert("replaced string: " + oStr);
changeStr(oStr, "something new"); alert("changed string: " + oStr);
JRS: In article <11************ *********@f6g20 00cwb.googlegro ups.com>,
dated Mon, 5 Jun 2006 18:14:09 remote, seen in
news:comp.lang. javascript, gary <gb*****@gmail. com> posted : How would one make the ECMA-262 String.replace method work with a string literal?
For example, if my string was "HELLO[WORLD]" how would I make it work in this instance.
Please note my square brackets are not regular expression syntax.
I assume that you want to use the above string as part of the RegExp, so
that a matching section can be found in a longer string.
S0 = "HELLO[WORLD]" // yours
S1 = S0.replace(/([\[\]])/g, "\\$1") // fix-up
T0 = "aaaHELLO[WORLD]cccxxxHELLO[WORLD]yyy" // test text
RE = new RegExp(S1)
T1 = T0.replace(RE, "bbb")
RE = new RegExp(S1, "g")
T2 = T0.replace(RE, "bbb")
A = [T1,,,T2] // results
No doubt you will need to fix up any other troublesome characters in S0,
by enhancing the line that gives S1.
Possibly, one could alternatively fix-up by converting characters to
Unicode???
*** DO NOT MULTI-POST ***
*** READ THE news:C.L.J FAQ ***
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian |
last post by:
I want to use regxp to check that a form input contains at least 1
non-space charcter. I'd like to only run this if the browser supports
it. For DOM stuff, I'd use
if (documentGetElementById) {}
Is there an object/feature detection I can use to check for regxp string
manipulation support?
--
|
by: Michael Hiegemann |
last post by:
Hello,
I am unaware whether this is the right group to ask. Please point me
to another forum if required.
I would like to replace a Fortran function by one which is written in
C. The function shall provide the same interface for the fortran code
calling the library. Fortunately, this works - currently under WinNT -
well for all functions which do not return a string, e.g.
|
by: Hilary Cotter |
last post by:
I'm trying to replace the characters in a pointer from an url string.
Here is my code.
// string has embedded '+', this code will not work.
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
{
char *p = szQuery;
while (*p++)
|
by: jgabbai |
last post by:
Hi,
What is the best way to white list a set of allowable characters using
regex or replace? I understand it is safer to whitelist than to
blacklist, but am not sure how to go about it.
Many thanks!
|
by: Evan |
last post by:
Hey, I posted this yesterday, but no one had any ideas? C'mon now, I
know this isn't that hard, i'm just a little new to javascript, and I
can't quite figure this out. I searched and searched to try and find
the answer to this, but I had no luck anywhere. It's a little
different situation than I found anywhere else. I would simply put
javascript into the <a> tag, but like I said, It's delivered via PHP,
and I don't have access to it. I...
| |
by: xla76 |
last post by:
Can anyone help me, I'm trying to replace any double quotes in a
textbox with a different character.
mystring = Replace(TextBox.Text , " , "*")
I've tried chr(34) and """"" with no luck.
Thanks
Pete
|
by: NvrBst |
last post by:
I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":
---------------------------------
myStringVar = myStringVar.replace("^" + iName + "=,($|&)", "");
---------------------------------
The following DOES replace it though:
---------------------------------
var match = myStringVar.match("^" + iName + "=,($|&)");
|
by: magix |
last post by:
Hi,
I have
char* str1 = "d:\temp\data\test.txt"
I want to replace all the "\" to be "\\", so that
the string will have "d:\\temp\\data\\test.txt"
Can you help ?
|
by: Ned White |
last post by:
Hi All,
To replace some substrings in a string value, i use Regex.Replace method
mulptiple times like;
strmemo = "new Data for user; Name:@@Name , Surname:@@Surname,
Address:@@Address";
strmemo = Regex.Replace(strmemo,"@@Name", value1.ToString());
strmemo = Regex.Replace(strmemo,"@@Surname", value2.ToString());
strmemo = Regex.Replace(strmemo,"@@Address", value3.ToString());
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |