I have the following string: "\IonSubsystem1\TRAN-88311"
I need to pull off the last word:TRAN-88311 which is an id, in a generic way.
How can I do it?
I have thought maybe found the last occurrence of '\' letter, but it is appear to me that I can't run the following order in JS :
var a = "\\IonSubsystem1\TRAN-88311";
a.lastIndexOf('\') ==> error in here.
Any other solution is welcome , thanks in advance
This is very easy.
var a = "\\IonSubsystem1\\TRAN-88311";
a = a.split("\\");
console.log(a[a.length-1]);
In your string representation, the \T is converted as a tab character.
This should do it:
var a = "\\IonSubsystem1\\TRAN-88311";var b = a.split('\\');console.log(b[b.length-1]);
Using Regex with capturing groups here is a solution.
var a = '\\IonSubsystem1\\TRAN-88311';
var id = a.replace(/(\\.*\\)(.*)/g, '$2');
alert(id);
Of course you have to escape that backslash otherwise the apex will be escaped instead (throwing the error)
Wrong
x.indexOf('\'); //The first apex opens the string, the slash will prevent the second apex to close it
Correct
x.indexOf('\\'); //The first apex open the string, the first backslash will escape the second one, and the second apex will correctly close the string
Related
Suppose a field(version) contains value 1.12.34 or 10.2.3.5 or any string which contains . at any position. So is there is any way to get the document by searching for 11234 or 10235, basically without . in the string.
You can use regex to solve this problem
var str = '1.12.34'
var newString = str.replace(/[^0-9]+/ig, "");
console.log(newString)
Here is an example.
So you can use your version field with replace function using the regex, don't need another field to store version value without dot(.)
I have values seperated by pipes in a database. But the issue is that I am appending | at every entry.
For Example:
|275634|374645|24354|
How can I remove the first pipe from the whole string not all the pipes.
Once inserted I don't need to check for the next time when it updates.
If I use substring(1) then it will remove the first character every time,
Please suggest a fix?
//input = '|275634|374645|24354|';
output = input.replace('|', '');
String#replace will replace the first occurance in a String. If you replace it with an empty String '' it is removed.
jsFiddle
you can use substring(index) method where ever you want to remove the perticular special character from the String: like
String str2 = "|275634|374645|24354|";
str2 = str2.substring(1);
System.out.println(str2);
you can see the output as 275634|374645|24354|
Any working Regex to find image url ?
Example :
var reg = /^url\(|url\(".*"\)|\)$/;
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
var string2 = 'url(http://domain.com/randompath/random4509324041123213.jpg)';
console.log(string.match(reg));
console.log(string2.match(reg));
I tied but fail with this reg
pattern will look like this, I just want image url between url(" ") or url( )
I just want to get output like http://domain.com/randompath/random4509324041123213.jpg
http://jsbin.com/ahewaq/1/edit
I'd simply use this expression:
/url.*\("?([^")]+)/
This returns an array, where the first index (0) contains the entire match, the second will be the url itself, like so:
'url("http://domain.com/randompath/random4509324041123213.jpg")'.match(/url.*\("?([^")]+)/)[1];
//returns "http://domain.com/randompath/random4509324041123213.jpg"
//or without the quotes, same return, same expression
'url(http://domain.com/randompath/random4509324041123213.jpg)'.match(/url.*\("?([^")]+)/)[1];
If there is a change that single and double quotes are used, you can simply replace all " by either '" or ['"], in this case:
/url.*\(["']?([^"')]+)/
Try this regexp:
var regex = /\burl\(\"?(.*?)\"?\)/;
var match = regex.exec(string);
console.log(match[1]);
The URL is captured in the first subgroup.
If the string will always be consistent, one option would be simply to remove the first 4 characters url(" and the last two "):
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
// Remove last two characters
string = string.substr(0, string.length - 2);
// Remove first five characters
string = string.substr(5, string.length);
Here's a working fiddle.
Benefit of this approach: You can edit it yourself, without asking StackOverflow to do it for you. RegEx is great, but if you don't know it, peppering your code with it makes for a frustrating refactor.
I have a string and I need to fix it in order to append it to a query.
Say I have the string "A Basket For Every Occasion" and I want it to be "A-Basket-For-Every-Occasion"
I need to find a space and replace it with a hyphen. Then, I need to check if there is another space in the string. If not, return the fixed string. If so, run the same process again.
Sounds like a recursive function to me but I am not sure how to set it up. Any help would be greatly appreciated.
You can use a regex replacement like this:
var str = "A Basket For Every Occasion";
str = str.replace(/\s/g, "-");
The "g" flag in the regex will cause all spaces to get replaced.
You may want to collapse multiple spaces to a single hyphen so you don't end up with multiple dashes in a row. That would look like this:
var str = "A Basket For Every Occasion";
str = str.replace(/\s+/g, "-");
Use replace and find for whitespaces \s globally (flag g)
var a = "asd asd sad".replace(/\s/g,"-");
a becomes
"asd-asd-sad"
Try
value = value.split(' ').join('-');
I used this to get rid of my spaces. Instead of the hyphen I made it empty and works great. Also it is all JS. .split(limiter) will delete the limiter and puts the string pieces in an array (with no limiter elements) then you can join the array with the hyphens.
I have a string of text "AB-123-2011-07-09", and need to remove everything except "123", then add a "#" sign to the end result.
The string "123" is ever increasing in number, as is the "2011-07-09" (a date). Only "AB" stays the same.
So the end result would be: #123
Is this possible?
Thanks.
EDIT: Just to clarify, I was needing a script that could globally search a page and replace any text which had the format of "AB-xxx-xxxx-xx-xx" with just the digits highlighted here in bold, then adding the "#" before it.
Currently there are only 3 digits in that position, but in the future there may be four.
My code:
function Replace() {
var OldString = "AB-123-2011-07-09";
var NewString = OldString.replace(/^AB-(\d+)-.*/, "#$1");
document.body.innerHTML = document.body.innerHTML.replace(OldString, NewString);
}
window.onload = Replace();
So far it only replaces 1 instance of the string, and uses a fixed string ("AB-123-2011-07-09").
What regular expression do I need to make the 'OldString' dynamic, rather than it being fixed as it is now?
var data = "AB-123-2011-07-09";
var field = data.split('-')[1];
document.write("#" + field);
http://jsfiddle.net/efortis/8acDr/
The following regex would work, but in this case I don't think you need a regex at all (as #Eric has already shown).
"AB-123-2011-07-09".replace(/^AB-(\d+)-.*/, "#$1");
This results in the value #123
http://jsfiddle.net/3XhbE/
Does this work?
var result = mystring.replace(new RegExp(AB-([0-9]+)-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9], "g"),"#$1");
mystring is the "AB-123-2011-07-09" string and result would be "#123".
This is of course possible. This regex would do the trick:
“AB-123-2011-07-09“.replace(/^AB-(\d+)-\d+-\d+-\d+$/, “#$1“);
It also checks you given syntax and that there is nothing else in the string.
migg