I have a string - "hi#i#am#hum'#'an ";
I want to split the string for an operator #, but don't want to split the string which is under single quote.
So I want the result - ["hi","i","am","hum#an"]
Try
input.split( /#(?!')/ )
Demo
var output = "hi#i#am#hum'#'an ".split( /#(?!')/ ).map( s => s.replace(/'#'/g, "#") );
console.log( output );
Here is another solution, not a one liner though. First replace '#' with some temporary character. Then apply the split, and replace temp character with #.
var str = "hi#i#am#hum'#'an";
str = str.replace(/'#'/g, '&');
str = str.split('#');
str = str.map(s => s.replace(/&/g, '#'))
console.log(str);
Related
I have the next problem. I need to remove a part of the string before the first dot in it. I've tried to use split function:
var str = "P001.M003.PO888393";
str = str.split(".").pop();
But the result of str is "PO888393".
I need to remove only the part before the first dot. I want next result: "M003.PO888393".
Someone knows how can I do this? Thanks!
One solution that I can come up with is finding the index of the first period and then extracting the rest of the string from that index+1 using the substring method.
let str = "P001.M003.PO888393";
str = str.substring(str.indexOf('.')+1);
console.log(str)
You can use split and splice function to remove the first entry and use join function to merge the other two strings again as follows:
str = str.split('.').splice(1).join('.');
Result is
M003.PO888393
var str = "P001.M003.PO888393";
str = str.split('.').splice(1).join('.');
console.log(str);
You could use a regular expression with .replace() to match everything from the start of your string up until the first dot ., and replace that with an empty string.
var str = "P001.M003.PO888393";
var res = str.replace(/^[^\.]*\./, '');
console.log(res);
Regex explanation:
^ Match the beginning of the string
[^\.]* match zero or more (*) characters that are not a . character.
\. match a . character
Using these combined matches the first characters in the string include the first ., and replaces it with an empty string ''.
calling replace on the string with regex /^\w+\./g will do it:
let re = /^\w+\./g
let result = "P001.M003.PO888393".replace(re,'')
console.log(result)
where:
\w is word character
+ means one or more times
\. literally .
many way to achieve that:
by using slice function:
let str = "P001.M003.PO888393";
str = str.slice(str.indexOf('.') + 1);
by using substring function
let str = "P001.M003.PO888393";
str = str.substring(str.indexOf('.') + 1);
by using substr function
let str = "P001.M003.PO888393";
str = str.substr(str.indexOf('.') + 1);
and ...
I have some string that looks like this:
var string = popupLink(25, 'Any string')
I need to use a regular expression to change the number inside (note that this is a string inside of a larger string so I can't simply match and replace the number, it needs to match the full pattern, this is what I have so far:
var re = new RegExp(`popupLink\(${replace},\)`, 'g');
var replacement = `popupLink(${formFieldInsert.insertId},)`;
string = string.replace(re, replacement);
I can't figure out how to do the wildcard that will maintain the 'Any String' part inside of the Regular Expression.
If you are looking for a number, you should use \d. This will match all numbers.
For any string, you can use lazy searching (.*?), this will match any character until the next character is found.
In your replacement, you can use $1 to use the value of the first group between ( and ), so you don't lose the 'any string' value.
Now, you can simply do the following:
var newNumber = 15;
var newString = "var string = popupLink(25, 'Any string')".replace(/popupLink\(\d+, '(.*?)'\)/, "popupLink(" + newNumber + ", '$1')");
console.log(newString);
If you just need to change the number, just change the number:
string = string.replace(/popupLink\(\d+/, "popupLink(" + replacement);
Example:
var str = "var string = popupLink(25, 'Any string')";
var replacement = 42;
str = str.replace(/popupLink\(\d+/, "popupLink(" + replacement);
console.log(str);
If you really do have to match the full pattern, and "Any String" can literally be any string, it's much, much more work because you have to allow for quoted quotes, ) within quotes, etc. I don't think just a single JavaScript regex can do it, because of the nesting.
If we could assume no ) within the "Any String", then it's easy; we just look for a span of any character other than ) after the number:
str = str.replace(/(popupLink\()\d+([^)]*\))/, "$1" + replacement + "$2");
Example:
var str = "var string = popupLink(25, 'Any string')";
var replacement = 42;
str = str.replace(/(popupLink\()\d+([^)]*\))/, "$1" + replacement + "$2");
console.log(str);
I need your help,
How can I get the last value (sub-string) of a text string that has hyphens in it?
var instr = "OTHER-REQUEST-ALPHA"
...some processing here to get the outstr value
var outstr = "ALPHA"
Use String#split and Array#pop methods.
var instr = "OTHER-REQUEST-ALPHA";
console.log(
instr.split('-').pop()
)
Or use String#lastIndexOf and String#substr methods
var instr = "OTHER-REQUEST-ALPHA";
console.log(
instr.substr(instr.lastIndexOf('-') + 1)
)
Or using String#match method.
var instr = "OTHER-REQUEST-ALPHA";
console.log(
instr.match(/[^-]*$/)[0]
)
The simplest approach is to simply split the string by your delimeter (ie. -) and get the last segment:
var inString = "OTHER-REQUEST-ALPHA"
var outString = inString.split("-").slice(-1)[0]
That's it!
Use SPLIT and POP
"String - MONKEY".split('-').pop().trim(); // "MONKEY"
Or This
string2 = str.substring(str.lastIndexOf("-"))
If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"
I've tried below code but no luck.. Is there a correct way to implement this?
var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);
alert(string);
One option is:
var rep = { '[': '=', ']': '', ',': '&' };
var query = str.replace(/[[,\]]/g, el => rep[el] );
The delimiters are already there, it's just a matter of replacing one delimiter with another. Replace each [ with an =, replace each , with an &, and remove all ].
var str = "a[123],b[456],c[789]"
var string = str.replace(/([a-z])\[(\d+)],?/g, '$1=$2&').slice(0, -1);
alert(string);
Brute force way im not good at Regex. Just adding my thoughts
var str = "a[123],b[456],c[789]"
str = str.replace(/],/g, '&');
str = str.replace(/\[/g, '=');
str = str.replace(/]/g,'');
alert(str);
The simple 2 line answer for this is:
str=str.replace(/,/g,"&");
str=str.replace(/(\w)\[(\d+)\]/g,"$1=$2");
I have a string |0|0|0|0
but it needs to be 0|0|0|0
How do I replace the first character ('|') with (''). eg replace('|','')
(with JavaScript)
You can do exactly what you have :)
var string = "|0|0|0|0";
var newString = string.replace('|','');
alert(newString); // 0|0|0|0
You can see it working here, .replace() in javascript only replaces the first occurrence by default (without /g), so this works to your advantage :)
If you need to check if the first character is a pipe:
var string = "|0|0|0|0";
var newString = string.indexOf('|') == 0 ? string.substring(1) : string;
alert(newString); // 0|0|0|0
You can see the result here
str.replace(/^\|/, "");
This will remove the first character if it's a |.
var newstring = oldstring.substring(1);
If you're not sure what the first character will be ( 0 or | ) then the following makes sense:
// CASE 1:
var str = '|0|0|0';
str.indexOf( '|' ) == 0 ? str = str.replace( '|', '' ) : str;
// str == '0|0|0'
// CASE 2:
var str = '0|0|0';
str.indexOf( '|' ) == 0? str = str.replace( '|', '' ) : str;
// str == '0|0|0'
Without the conditional check, str.replace will still remove the first occurrence of '|' even if it is not the first character in the string. This will give you undesired results in the case of CASE 2 ( str will be '00|0' ).
Try this:
var str = "|0|0|0|0";
str.replace(str.charAt(0), "");
Avoid using substr() as it's considered deprecated.
It literally is what you suggested.
"|0|0|0".replace('|', '')
returns "0|0|0"
"|0|0|0|0".split("").reverse().join("") //can also reverse the string => 0|0|0|0|