javascript regex replace not working in apache camel - javascript

I am using apache camel 2.11 and camel-script 2.2 for javascript support with spring DSL.
I wanted to do a regex replace with groups, but that does not seem to work in camel.
Eg :- in a transform, the following works
<javaScript>request.body.toString().replace("Y","X")</javaScript>
i.e Change YZ to XZ
But the following does not :-
<javaScript>request.body.toString().replace("/(Y)(.*)$/g","\$1")</javaScript>
ie. Change YZXXX to Y
I have tested the regex itself at https://regex101.com/, so I don't believe its a syntax issue in the regex itself
Now it may be possible that camel-script offers only partial javascript support. but I am not sure if that is the case. any ideas?

I found the issue :-)
The issue is :- I was assuming that Camel javascript can use javascript functions... when in fact it calls java functions.
Changing the code to java syntax ( calling replaceAll() function with java syntax for regex) resolved the issue.

You need to use a regex, not a string looks like a regex. Compare:
Wrong:
request.body.toString().replace("/(Y)(.*)$/g","\$1")
Correct:
request.body.toString().replace(/(Y)(.*)$/g, "\$1")
That being said, the backslash in your replace pattern is superfluous. "$1" would be correct.

Related

Why does my regular expression work in PHP but not JavaScript?

I have a regex created by myself that I am currently running in PHP. Although when I merge it over to JavaScript, it refuses to work. I have also tried it in Python and it works perfectly fine.
Regex:
#[[](.[^]]+)[]][()](\d+)[)]
Testing in PHP, and working
Testing in JavaScript, and not working
JavaScript doesn't automatically escape your ].
This will help you get a visual idea:
PCRE:
JS:
Python:
So to fix this, you need to escape the brackets
#[[](.[^\]]+)[\]][()](\d+)[)]
// ^ ^
The best way to write this regex is to minimize the use of character classes:
#\[(.[^\]]+)\][()](\d+)\)
That's why it's good practice to escape this stuff instead of relying on quirks of the flavor.
I generated these images through regex101.

I have problems in use unicode in java scriptn

I'm beginner in Javascript, I have a url containing unicode like below:
/Solutions/راه-کار-جامع-امنیت-اطلاعات
Now I need to read the path name by following code
window.location.pathname.split('/')
and in the output I have this
"", "Solutions", "%D8%B1%D8%A7%D9%87-%DA%A9%D8%A7%D8%B1-%D8%AC%D8%A7…%D8%AA->%D8%A7%D8%B7%D9%84%D8%A7%D8%B9%D8%A7%D8%AA"
How can I solve this problem?
The unicode text is url-encoded. This means that the unicode characters are translated to codes that are safe to use as a url. You can revert this using the decodeURIComponent or the decodeURI function.
The difference between these two is already nicely explained in this question. In your case you will most likely use decodeURIComponent after you performed the split.

Java Regex replace function not working as intended

I need some help with a JS Regex.
Here's the string I'm passing, I want to delete everything before 'Hanyuu-sama' with JS Replace.
Hanyuu","dj":{"id":18,"djname":"Hanyuu-sama
The first and second "Hanyuu" can change, the id number can change. This has already been cropped quite a bit with regular expressions.
Now I've tried a few and surprisingly it's failing when I do simple and complex regexes:
I've tried:
.*\"
And it does nothing, I've tried disgusting stuff in my desperation:
.*\","dj\":{\"id":.*,\"djname\":\"
And nada.
Here's a JS Fiddle and here's a http://regex101.com/r/tE2uY0/1 Regex JS matching platform.
Does anyone know why this isn't working?
I know this is likely bad practice, I'm just trying to learn Regexes.
Bonus points if anyone can refer me to a good source to learn Regular expressions. I'd love a solution but I'd like to learn how to do this myself in the future and why this one failed even more.
Your method call should look like this:
source = source.replace(/.*"/, "");
Regular expression in javascript are written between /.../ and not "/.../" like they are in many other languages.
If your string is always structured like that and it does not contain any more characters, your regex should do the trick. That's because the * quantifier acts greedy by default, thus always matching the last " in the string.

Does Python's "\x00" exist in javascript?

I'm currently working on transitioning from Python to Node.js, but I can't figure out if there is an easy way to add a non-printable character to a string.
In Python it would look something like this:
conn.send("\x01SomeMoreTextHere")
But in Javascript I can't seem to find anything simpler than
conn.write(fromCharCode(parseInt("0x01")) + "SomeMoreTextHere")
Or if you are fine with using decimals, in this specific case it doesn't matter to me, you can skip the parseInt part.
I can't find any other way that happens to be as simple as the Python way, am I not looking hard enough?
Yes, Javascript supports \xdd escape sequences
- Martijn Peters
Looks like the answer to me.
Escape sequences
Also MDN:
String literals
hex, octal, unicode...

Best way to generate javascript code in ruby (RoR)

I have seen some rails plugins which generate javascript code dynamically using ruby.
1.
%Q ( mixed block of javascript and ruby )
2.
<<-CODE
some mixed ruby and javascript code
CODE
Being a java developer I don't understand
what those strange looking syntax mean ?
Is one way better than the other ?
can anyone point me to proper documentation about such things ?
The first syntax is Ruby's string literal syntax. Specifically, the %Q (capital Q as opposed to lower-case) means that the string will be interpolated. eg:
%Q[Here's a string with #{a_variable} interpolated!]
Note that you can use any arbitrary characters as the open and close delimiters.
The second syntax is Ruby's heredoc syntax. The dash after the opening << indicates that Ruby will strip whitespace from the beginning of input lines contained in the heredoc block.
Ruby on Rails ships with the Prototype JavaScript framework built-in already. It also ships with JS generator helper methods which generate the Prototype code dynamically based on Ruby code.
You needn't use these if you don't want to. In fact, I rarely use them or Prototype at all, as jQuery is my JS framework of choice. So one way is not "better" than the other (except in the general sense that heredoc is better than the string literal syntax for certain cases).
In Ruby %Q provides a double quote delimited string, so:
%Q(mixed block of javascript and ruby) #=> "mixed block of javascript and ruby"
<<-CODE is what Ruby calls a Here Document, or simply heredoc. This is a mechanism for creating free format strings whilst preserving special characters such as new lines and tabs.
A heredoc is created by preceding the text with << followed by the delimiter string you wish to use to mark the end of the text.
text = <<-DOC
To be, or not to be: that is the question
William Shakespeare
DOC
When this string is printed it appears exactly as it was entered, together with all the new lines and tabs:
To be, or not to be: that is the question
William Shakespeare
%Q is the equivalent to a "" string in Ruby. But if you use such %Q-syntax, you don't need to escape double quotes.
It's a HEREDOC declaration. You also don't need to escape quotes there.
Strings in Ruby.
Here you can find the details.
Ruby with javascript

Categories

Resources