Format numeric string - javascript

I've the following string (meaning it's not numeric):
"0870490055012000000000"
wich is always 22 characters long. I need to transform into:
"087.049.0055.0120.0000.0000"
Using PHP or even on js/client side.
I found something like this but was not able to solve the problem.
I guess there are many ways to solve this. I'm just asking for something like:
$x= format("00000", "xxx.xxx..xxx.x.x.x")
or
$x = preg_replace("/a;;w.;w;e;ew")

with PHP, you can use this:
$str = preg_replace('~\A\d{3}\K\d{3}|\d{4}~', '.$0', $str);
where \A is an anchor for the start of the string.
\K removes all that have been matched on the left from match result.
If you need something more general to apply a mask to a string, the link you shared in your question will give you the way to do.

Related

JavaScript split string by specific character string

I have a text box with a bunch of comments, all separated by a specific character string as a means of splitting them to display each comment individually.
The string in question is | but I can change this to accommodate whatever will work. My only requirement is that it is not likely to be a string of characters someone will type in an everyday sentence.
I believe I need to use the split method and possibly some regex but all the other questions I've seen only seem to mention splitting by one character or a number of different characters, not a specific set of characters in a row.
Can anyone point me in the right direction?
.split() should work for that purpose:
var comments = "this is a comment|and here is another comment|and yet another one";
var parsedComments = comments.split('|');
This will give you all comments in an array which you can then loop over or do whatever you have to do.
Keep in mind you could also change | to something like <--NEWCOMMENT--> and it will still work fine inside the split('<--NEWCOMMENT-->') method.
Remember that split() removes the character it's splitting on, so your resulting array won't contain any instances of <--NEWCOMMENT-->

RegEx that works in Javascript won't do so in PHP

I will try to make my question short yet understandable, I have a simple RegEx I use in javascript to check for characters that aren't alphanumeric (AKA Symbols). It would be "/[$-/:-?{-~!"^_`[]]/"
In javascript, doing
if(/[$-/:-?{-~!"^_`\[\]]/.test( string ))
just works, if any of those characters are in the string, it will give true, else, it will give false. I tried to do the same in PHP, the following way
if(preg_match('/[$-/:-?{-~!"^_`\[\]]/', $string ))
other regexes work when done this way, but this particular one simply will give false no matter what when ran in PHP.
Is there any reason to this? Am I doing something wrong? Does PHP comprehend regexes in a different way? What should I change to make it work?
Thanks for your time.
Since php uses PCRE, you will get a pattern error using delimiter / as seen here http://regex101.com/r/3ILGgE/1
So, it should be escaped correctly.
Using / as the delimiter, the string is
'/[$-\/:-?{-~!"^_`\[\]]/'
Using ~ as the delimiter, the string is
'~[$-/:-?{-\~!"^_`\[\]]~'
Also, be aware you have a couple of range's in the class $-/ and :-? and {-~
that will include the characters between the from/to range characters as well
and does not include the range character - itself as it is an operator.

regex replace on JSON is removing an Object from Array

I'm trying to improve my understanding of Regex, but this one has me quite mystified.
I started with some text defined as:
var txt = "{\"columns\":[{\"text\":\"A\",\"value\":80},{\"text\":\"B\",\"renderer\":\"gbpFormat\",\"value\":80},{\"text\":\"C\",\"value\":80}]}";
and do a replace as follows:
txt.replace(/\"renderer\"\:(.*)(?:,)/g,"\"renderer\"\:gbpFormat\,");
which results in:
"{"columns":[{"text":"A","value":80},{"text":"B","renderer":gbpFormat,"value":80}]}"
What I expected was for the renderer attribute value to have it's quotes removed; which has happened, but also the C column is completely missing! I'd really love for someone to explain how my Regex has removed column C?
As an extra bonus, if you could explain how to remove the quotes around any value for renderer (i.e. so I don't have to hard-code the value gbpFormat in the regex) that'd be fantastic.
You are using a greedy operator while you need a lazy one. Change this:
"renderer":(.*)(?:,)
^---- add here the '?' to make it lazy
To
"renderer":(.*?)(?:,)
Working demo
Your code should be:
txt.replace(/\"renderer\"\:(.*?)(?:,)/g,"\"renderer\"\:gbpFormat\,");
If you are learning regex, take a look at this documentation to know more about greedyness. A nice extract to understand this is:
Watch Out for The Greediness!
Suppose you want to use a regex to match an HTML tag. You know that
the input will be a valid HTML file, so the regular expression does
not need to exclude any invalid use of sharp brackets. If it sits
between sharp brackets, it is an HTML tag.
Most people new to regular expressions will attempt to use <.+>. They
will be surprised when they test it on a string like This is a
first test. You might expect the regex to match and when
continuing after that match, .
But it does not. The regex will match first. Obviously not
what we wanted. The reason is that the plus is greedy. That is, the
plus causes the regex engine to repeat the preceding token as often as
possible. Only if that causes the entire regex to fail, will the regex
engine backtrack. That is, it will go back to the plus, make it give
up the last iteration, and proceed with the remainder of the regex.
Like the plus, the star and the repetition using curly braces are
greedy.
Try like this:
txt = txt.replace(/"renderer":"(.*?)"/g,'"renderer":$1');
The issue in the expression you were using was this part:
(.*)(?:,)
By default, the * quantifier is greedy by default, which means that it gobbles up as much as it can, so it will run up to the last comma in your string. The easiest solution would be to turn that in to a non-greedy quantifier, by adding a question mark after the asterisk and change that part of your expression to look like this
(.*?)(?:,)
For the solution I proposed at the top of this answer, I also removed the part matching the comma, because I think it's easier just to match everything between quotes. As for your bonus question, to replace the matched value instead of having to hardcode gbpFormat, I used a backreference ($1), which will insert the first matched group into the replacement string.
Don't manipulate JSON with regexp. It's too likely that you will break it, as you have found, and more importantly there's no need to.
In addition, once you have changed
'{"columns": [..."renderer": "gbpFormat", ...]}'
into
'{"columns": [..."renderer": gbpFormat, ...]}' // remove quotes from gbpFormat
then this is no longer valid JSON. (JSON requires that property values be numbers, quoted strings, objects, or arrays.) So you will not be able to parse it, or send it anywhere and have it interpreted correctly.
Therefore you should parse it to start with, then manipulate the resulting actual JS object:
var object = JSON.parse(txt);
object.columns.forEach(function(column) {
column.renderer = ghpFormat;
});
If you want to replace any quoted value of the renderer property with the value itself, then you could try
column.renderer = window[column.renderer];
Assuming that the value is available in the global namespace.
This question falls into the category of "I need a regexp, or I wrote one and it's not working, and I'm not really sure why it has to be a regexp, but I heard they can do all kinds of things, so that's just what I imagined I must need." People use regexps to try to do far too many complex matching, splitting, scanning, replacement, and validation tasks, including on complex languages such as HTML, or in this case JSON. There is almost always a better way.
The only time I can imagine wanting to manipulate JSON with regexps is if the JSON is broken somehow, perhaps due to a bug in server code, and it needs to be fixed up in order to be parseable.

How do I convert domain.com/foo/bar/baz/ into a string like 'foo bar baz'?

Basically I want to be able to grab the ending of an url, and convert it into a string to be used somewhere.
Currently I'm doing this (which is less than optimal):
// grab the path, replace all the forward slashes with spaces
local_path = location.pathname.toString().replace(/\//g,' ');
// strip empty spaces from beginning / end of string
local_path.replace(/^\s+|\s+$/g,""));
But I think there is probably a better way. Help?
Edit: Could I confidently get rid of the .toString method there?
You could do something like this if you want to avoid regular expressions:
location.pathname.substring(1).split('/').join(' ')
That will get rid of the initial slash, but won't take care of a trailing slash. If you need to deal with those, you can omit substring and use trim for modern implementations or a regex:
location.pathname.split('/').join(' ').replace(/^\s+|\s+$/g, '')
What's wrong with what you have? Looks fine to me. That is the easiest way to handle what you want to do.
You could use the regex provided by Douglas Crockford on http://www.coderholic.com/javascript-the-good-parts/ and then split the path at the forward-slash.

Regexp with variables: how to convert xml with attributes to html using javascript replace method?

Apologies if this is a dup, I searched but couldn't quite find the info I was looking for.
Using javascript, I want to search a string, find a tag, match attributes in the tag, and store them as variables for rewriting. Here's the part of the string I'm looking for:
<my_child name="view" gso="g--" type="Application.View">
that I'd like to convert to:
<tr><td>view</td><td>Appication.View</td><td>g--</td></tr>
Here's the regexp I'm trying. I don't think it's actually finding a match though:
objString = objString.replace(/<my_child name="(.*)" gso="(.*)" type="(.*)">/g, '<tr><td>'+RegExp.$1+'</td><td>'+RegExp.$3+'</td><td>'+RegExp.$2+'</td><td>');
EDIT: SOLVED.
Thanks for the advice kiamlaluno. Turns out I was also not taking into account the indeterminate number of spaces between attributes. Here's the updated regexp:
/<my_child[ ]+name="([^"]*)"[ ]+gso="([^"]*)"[ ]+type="([^"]*)"[ ]*>/g, '<tr><td>$1</td><td>$3</td><td>$2</td><td>'
you can replace
'<tr><td>'+RegExp.$1+'</td><td>'+RegExp.$3+'</td><td>'+RegExp.$2+'</td><td>'
with
'<tr><td>$1</td><td>$3</td><td>$2</td><td>'
and the (.*) matches should probably be ([^"]*) instead
maybe that will help get you closer?

Categories

Resources