need help with regular expression in javascript replace function

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.
rutledgjAsked:
Who is Participating?
 
tolgaongCommented:
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
 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:
Maybe if you try the complete message. It leaves the S in there when I do it.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
HonorGodSoftware EngineerCommented:
Ah, that's why it doesn't work.

\S

is actually represented as:

S

in the string, and

\9

is:

9
0
 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:

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
 
rutledgjAuthor Commented:
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
 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:
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
 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:
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
 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:
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
 
HonorGodSoftware EngineerCommented:
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
 
rutledgjAuthor Commented:
Yes. So I'm out of luck?
0
 
käµfm³d 👽Commented:
How are you loading this data into javascript?
0
 
HonorGodSoftware EngineerCommented:
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
 
tolgaongCommented:
//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
 
tolgaongCommented:
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
 
rutledgjAuthor Commented:
Thanks but this doesn't get the ^ in there to replace the S
0
 
tolgaongCommented:
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
 
rutledgjAuthor Commented:
Guess I'm missing something.  The S stays in there. That is what needs to be replaced with a ^
0
 
tolgaongCommented:
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
 
rutledgjAuthor Commented:
I tried both of these and neither replace the S
0
 
käµfm³d 👽Commented:
You said that this data is not being loaded to a webpage. Can you clarify how javascript comes into the picture?
0
 
rutledgjAuthor Commented:
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
 
käµfm³d 👽Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.