Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

need help with regular expression in javascript replace function

Posted on 2011-05-11
30
Medium Priority
?
510 Views
Last Modified: 2012-06-21
I have this text:

OBX|9|NM|656158^PLT||225|10\S\9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&";

I need to replace the \S\  that appears here:  10\S\9/L  with a caret so I'd have 10^9/L

I want to get rid of all of the \S\ and replace with ^ so I need a global replacement function.

I tried this:  tmpStr = tmpStr.replace(/\\S\\/g,"^");

which gets rid of the \  but not the S.
0
Comment
Question by:rutledgj
  • 12
  • 9
  • 5
  • +1
30 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 35737869
When I try it, the replace works for me

Output:

10^9/L
<html>
<body>

<script type="text/javascript">

var t = '10\\S\\9/L'
t = t.replace( /\\S\\/g, '^' )
document.write( t );

</script>

</body>
</html>

Open in new window

0
 

Author Comment

by:rutledgj
ID: 35737886
Maybe if you try the complete message. It leaves the S in there when I do it.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35737963
Like this?

output:

OBX|9|NM|656158^PLT||225|10^9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&"
<html>
<body>

<script type="text/javascript">


var t = 'OBX|9|NM|656158^PLT||225|10\\S\\9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&"'

t = t.replace( /\\S\\/g, '^' )
document.write( t );


</script>

</body>
</html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rutledgj
ID: 35737987
You have an extra slash in the original message.  Try this

var t = 'OBX|9|NM|656158^PLT||225|10\S\9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&';
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35738076
If you do an "alert(t)", you will see that there is no slash before the "S" due to escaping. Perhaps the correct string is?

var t = 'OBX|9|NM|656158^PLT||225|10\\S\\9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&';

Open in new window


Assuming it is, then HonorGod's first post should be what you need.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35738082
Ah, that's why it doesn't work.

\S

is actually represented as:

S

in the string, and

\9

is:

9
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35738112
The output of the following is:

25 = "1"
26 = "0"
27 = "S"
28 = "9"
29 = "/"
<html>
<body>

<script type="text/javascript">

var t = 'OBX|9|NM|656158^PLT||225|10\S\9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&';

for ( var i = 25; i < 30; i++ ) {
  document.write( i + ' = "' + t.charAt( i ) + '" <br/>' )
}

</script>

</body>
</html>

Open in new window

0
 

Author Comment

by:rutledgj
ID: 35738124

This is what I need


Original Msg

OBX|9|NM|656158^PLT||225|10\S\9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&

I need it to look like this after correction

OBX|9|NM|656158^PLT||225|10^9/L|140-444|||U|F|||20110503145020|Undefined Providence Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&
0
 

Author Comment

by:rutledgj
ID: 35738145
The original text is an HL7 message. It is not going through a web page so the characters inside the message are not escape characters.  This whole message should be viewed as valid line of text between quotes.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35738199
Would this be sufficient?:

tmpStr = tmpStr.replace(/S/g,"^");

Or must the S be followed by a digit?

tmpStr = tmpStr.replace(/S(\d)/g,"^$1");
0
 

Author Comment

by:rutledgj
ID: 35738262
This appears to replace all S in a string. If I add an S somewhere else in the string without the slash marks it removes them also.  Doesn't matter if there is a digit or another letter or pipe or caret after the \S\.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35738342
The problem is that when the original message is put into a JavaScript string variable, the

\S\9

is actually stored as

S9

So, we need to find out the "true" pattern of what can and should occur around this S so that we can correctly replace it.

Is it always in the same position?  i.e., offset 27?

Or can the S occur elsewhere?
0
 

Author Comment

by:rutledgj
ID: 35738404
What I'm working with doesn't see the mesage like that.  All characters are treated as text (forgetting javascrpt).

I'm using a tool that listens for these HL7 messages and processes them.  In a normal HL7 message the ^ is used as a field separator.  For some reason the sending system is sending '\S\' instead of the ^.  It can appear anywhere in the message.

Perhaps javascript is doing something weird with it. I don't know how to tell.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35738505
If he backslash actually exists in the JavaScript string, then it would be represented as we saw earlier:

'10\\S\\9/L'

So, if we output (using something like alert(), or document.write()), we would expect to see it as:


10\S\9/L

However, if the string is displayed as:

10S9/L

Then, the backslash is, essentially, ignored, and is therefore removed.

Does that make any sense?
0
 

Author Comment

by:rutledgj
ID: 35738578
I understand what you are saying. But if I have a string like this:

10\S\9/L|My^Name^Is^Sue

does javascript store then \S\ the same as the S in Sue?  And regardless of what javascript does, I guess the bottom line is how to remove the \S\ and not the sings S in a string.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35738817
Q: does javascript store then \S\ the same as the S in Sue?
A: it depends upon how the value is put into the JavaScript variable.

Q: ... how to remove the \S\ and not the sings S in a string?
A: If it it actually stored as \\S\\ (i.e., the backslash characters are present), then you would do as shown above.  It that doesn't work, then it means that the \S is stored in the string as S, so you can't distinguish between:

\S\9

and

S9

Does that make sense?
0
 

Author Comment

by:rutledgj
ID: 35738839
Yes. So I'm out of luck?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35739063
How are you loading this data into javascript?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35739419
Well, you are out of luck unless we can identify a pattern, or specific location within the string over which the change should be made.
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 35744231
//also shown without escape sequences.
var data='|10\\S\\9/L| '; // |10\S\9/L| 
//var data='|10\\\\S\\9/L| '; // |10\\S\9/L| 
//var data='|10\\S\\\\9/L| '; // |10\S\\9/L| 
//var data='|10\\\\S\\\\9/L| '; // |10\\S\\9/L|  
result = subject.replace(/(\\)\1*([^\\]+)\\\1*/ig, "$2");

Open in new window


Results:
|10\S\9/L| - > |10S9/L|
|10\\\ABC\9/L| -> |10ABC9/L|
|10\K\\\9/L| |10K9/L|
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 35744236
Sorry.

//also shown without escape sequences.
var data='|10\\S\\9/L| '; // |10\S\9/L| 
//var data='|10\\\\S\\9/L| '; // |10\\S\9/L| 
//var data='|10\\S\\\\9/L| '; // |10\S\\9/L| 
//var data='|10\\\\S\\\\9/L| '; // |10\\S\\9/L|  
var result = data.replace(/(\\)\1*([^\\]+)\\\1*/ig, "$2"); 

Open in new window

0
 

Author Comment

by:rutledgj
ID: 35745970
Thanks but this doesn't get the ^ in there to replace the S
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 35746079
try this...

//also shown without escape sequences.
var data='|10\\S\\9/L| '; // |10\S\9/L| 
//var data='|10\\\\S\\9/L| '; // |10\\S\9/L| 
//var data='|10\\S\\\\9/L| '; // |10\S\\9/L| 
//var data='|10\\\\S\\\\9/L| '; // |10\\S\\9/L|  
var result = data.replace(/(\\)\1*[^\\]+\\\1*/ig, "^"); 

Open in new window

0
 

Author Comment

by:rutledgj
ID: 35746114
Guess I'm missing something.  The S stays in there. That is what needs to be replaced with a ^
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 35746191
if the content can be vary between backslashes, use the code below.
var result = data.replace(/(\\)\1*[^\\]+\\\1*/ig, "^"); 

Open in new window


If the content is static and its always 'S' then please the code below.
var result = data.replace(/(\\)\1*S\\\1*/g, "^"); 

Open in new window

0
 

Author Comment

by:rutledgj
ID: 35746496
I tried both of these and neither replace the S
0
 
LVL 9

Accepted Solution

by:
tolgaong earned 2000 total points
ID: 35746722
rutledgi,

You have to look at escape sequences. If you enter the same data to an inputbox it will work. If you take data from an inputbox, you can see the code is working. If you are working with an dynamic data. You have addslash to data. Javascript addslashes, stripslashes functions can be seen below.

SOME ESCAPE SEQUENCE
\n New line - you cannot see the affect in html but if you want to test it use textarea object
\r Carriage return
\t Horizontal tab
\' Single quotation mark
\ " Double quotation mark
\\ Backslash

usage sample:
alert('rutledgi\'s example');
 
<html>
 <head>
  <script type = "text/javascript">
   function addslashes (text) {
	return text.replace(/[\u0000\u0008\u0009\u000A\u000D\u001A\u0022\u0027\u005C\u0060]/g, "\\$&");
   }

   function stripslashes (text) {
	return text.replace(/(\\)([\u0000\u0008\u0009\u000A\u000D\u001A\u0022\u0027\u005C\u0060])/g, "$2");
   }

   function ChangeS () {
	// c based languages escape sequences are important!. You have to correct the input. with addslashes function
	var correcteddata = document.getElementById('dataArea').value;

	var result = correcteddata.replace(/(\\)\1*[^\\]+\\\1*/ig, "^");
	document.getElementById('resultDiv').innerText = addslashes(correcteddata);
	document.getElementById('resultDivStripped').innerText = result;
   }
  </script>
 </head>

 <body onload = "ChangeS()">
  <b>Data</b>

  <br>
  <input type="text" size="140" id="dataArea"
  value="OBX|9|NM|656158^PLT||225|10\S\9/L|140-444|||U|F|||20110503145020|Undefined Providence
  Location|^BOLLETIO^TONI^^^^^^&&^^^^^&&";" />

  <input type = "button" value = "Change It" onclick = "ChangeS()" />

  <br>
  <br>
  <b>Javascript Variable (Escape Sequences)</b>
<div id = "resultDiv"></div>

  <br>
  <b>Regular Expression Result</b>
<div id = "resultDivStripped"></div>
 </body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35749623
You said that this data is not being loaded to a webpage. Can you clarify how javascript comes into the picture?
0
 

Author Comment

by:rutledgj
ID: 35750047
It is used in an open source application called MirthConnect. It is used to receive hl7 messages (medical data). Inside the application you can transform the message using javascript. The output is typically saved to a database or file.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35750728
Can you provide a sample of how you implemented the above suggestions (including sanitized Mirth code)?

In case you don't know, replacements in javascript are not in-place, so you'll have to capture the returned value from the replace function and assign it back to the existing object (or forward that value elsewhere).

For example, given the following:

var data = "hello world!";

Open in new window


Executing the following will result in "hello world!":

data.replace(/o/g, '');
alert(data);

Open in new window


Executing this will result in "hell wrld!":

data = data.replace(/o/g,'');
alert(data);

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question