How to achieve this result using Regex? - javascript

Given the input below, what's the regex expression that gives the desired output in javascript? I must achieve this without using a multiline flag.
input
\n
\n
abc def.\n
\n
*\n
\n
desired output (maintain same number of rows but insert = into blank rows)
=\n
=\n
abc def.\n
=\n
*\n
=\n
actual output (using regex /[^a-zA-Z0-9.*]+\n/ replaced with =\n; it somehow removes one of two consecutive `\n`s)
=\n
abc def.=\n
*=\n

You could try a combination of replace functions like so:
str = "\n\nabc def.\n\n*\n\n";
str = str.replace(/\n/g, "=\n");
str = str.replace(/(.)=\n/g, "$1\n");
console.log(str);
Explanation -
After the first replacement/s, the output looks like:
=
=
abc def.=
=
*=
=
Then, you replace any characters followed by a =\n and replace it with that same character (given by $1), followed by a newline.

Your desired outcome is "maintain same number of rows but insert = into blank rows".
An empty ("blank") row is a row that matches the regex: ^$.
^ means the beginning of the input string, $ means the end of the input string but if the m modifier is specified (it means "multi-line"), ^ matches the beginning of a line and $ matches the end of a line.
Your code should be as simple as:
input = "\n\nabc def.\n\n*\n\n";
output = str.replace(/^$/mg, '=');
The m modifier changes the meaning of ^ and $ as explained above. The newline characters are not matched by the regex above and consequently they do not need to be present in the replacement string.
The g modifier tells String.replace() to find and replace all the matching substrings, not only the first one (the default behaviour of String.replace()).
Read more about regular expressions in JavaScript.

This should work with two replace :
value.replace(/^\n/, '=\n').replace(/\n\n/g, '\n=\n')
The first replace takes care of the first line if it starts with a blank row.
The second replace takes care of other lines : adding = in blank rows is the same than inserting = between two consecutives \n

Related

Regex select all after nth occurance of character

I have a string in the following format and I'm trying to return all the data after the 3rd occurrence of the ':' character as in the example below.
user_name_1, 10:46:36 activity_name_1 : the text to be returned
So far I have the regex \:.* that returns everything after the first occurrence, eg. :46:36 activity_name_1 : the text to be returned
If I modify it to \:{3}.* eg. to look for the 3rd instance, the regex will not return any matches. It looks like it should be a very simple query but nothing I've tried seems to work.
I've already found the following question find value after nth occurence of - using RegEx however in this case they're returning only the next 3 digits after the nth character and not the entire remaining string.
You can use
^(?:[^:]*:){3}\s*(\S.*)
See the regex demo. Details:
^ - start of string
(?:[^:]*:){3} - three occurrences of any zero or more chars other than a : and then a : char
\s* - zero or more whitespaces
(\S.*) - Group 1: a non-whitespace char and then the rest of the line.
See the JavaScript demo:
const text = "user_name_1, 10:46:36 activity_name_1 : the text to be returned";
const match = text.match(/^(?:[^:]*:){3}\s*(\S.*)/)
if (match) {
console.log(match[1])
}
I'd suggest not using regex for this. split() the string by the : character and remove the first two elements of the resulting array.
You can turn the result back in to a string if necessary by using join():
let input = 'user_name_1, 10:46:36 activity_name_1 : the text to be returned : foo : bar';
let arr = input.split(':');
arr.splice(0, 3);
console.log(arr);
let output = arr.join(':').trim();
console.log(output);

extract all the telephone numbers from a string

I have a regex that is written to identify a telephone no.
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
What I want is to do to extract all the telephone numbers from a string and store it in an array. Yhis is what I did:
var rtel = new RegExp(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/gi)
var r = "Inthe (555)-555-5555 pavithrarox #gmail.com strings below, you 'll find that the content of each 1. abc . pavithraprbd#gmail.com line is indented by some whitespace from the index of the line (the number is a part of the text to match). Try writing a pattern that can match each line regardless of how much whitespace is between the number and the content. Notice that the whitespace characters are just like any other character and the special metacharacters like the star and the plus can be used as well.".match(rtel);
But this only matches if the full string matches the regex only. how do I get all the telephone numbers from the string. what am I missing
Remove the ^ (start) and $ (end) anchors in your regex. If you put these in, your entire string must match.
var anchors = new RegExp(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/gi);
var no_anchors = new RegExp(/(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}/gi);
var testString1 = "Inthe (555)-555-5555 pavithrarox #gmail.com strings below, you 'll find that the content of each 1. abc . pavithraprbd#gmail.com line is indented by some whitespace from the index of the line (the number is a part of the text to match). Try writing a pattern that can match each line regardless of how much whitespace is between the number and the content. Notice that the whitespace characters are just like any other character and the special metacharacters like the star and the plus can be used as well.";
var testString2 = "(555)-555-5555";
console.log("testString1 - anchors: ", testString1.match(anchors)) // null
console.log("testString1 - no_anchors: ", testString1.match(no_anchors)) // ['(555)-555-5555']
console.log("testString2 - anchors: ", testString2.match(anchors)) // ['(555)-555-5555']
console.log("testString2 - no_anchors: ", testString2.match(no_anchors)) // ['(555)-555-5555']

Javascript: Remove trailing chars from string if they are non-numeric

I am passing codes to an API. These codes are alphanumeric, like this one: M84.534D
I just found out that the API does not use the trailing letters. In other words, the API is expecting M84.534, no letter D at the end.
The problem I am having is that the format is not the same for the codes.
I may have M84.534DAC, or M84.534.
What I need to accomplish before sending the code is to remove any non-numeric characters from the end of the code, so in the examples:
M84.534D -> I need to pass M84.534
M84.534DAC -> I also need to pass M84.534
Is there any function or regex that will do that?
Thank you in advance to all.
You can use the regex below. It will remove anything from the end of the string that is not a number
let code = 'M84.534DAC'
console.log(code.replace(/[^0-9]+?$/, ""));
[^0-9] matches anything that is not a numer
+? Will match between 1 and unlimited times
$ Will match the end of the string
So linked together, it will match any non numbers at the end of the string, and replace them with nothing.
You could use the following expression:
\D*$
As in:
var somestring = "M84.534D".replace(/\D*$/, '');
console.log(somestring);
Explanation:
\D stands for not \d, the star * means zero or more times (greedily) and the $ anchors the expression to the end of the string.
Given your limited data sample, this simple regular expression does the trick. You just replace the match with an empty string.
I've used document.write just so we can see the results. You use this whatever way you want.
var testData = [
'M84.534D',
'M84.534DAC'
]
regex = /\D+$/
testData.forEach((item) => {
var cleanValue = item.replace(regex, '')
document.write(cleanValue + '<br>')
})
RegEx breakdown:
\D = Anything that's not a digit
+ = One or more occurrences
$ = End of line/input

Escape single backslash inbetween non-backslash characters only

I have some input coming in a web page which I will re display and submit elsewhere. The current issue is that I want to double up all single backslashes that are sandwiched inbetween non-backslash characters before submitting the input elsewhere.
Test string "domain\name\\nonSingle\\\WontBe\\\\Returned", I want to only get the first single backslash, between domain and name.
This string should get nothing "\\get\\\nothing\\\\"
My current pattern that I can get closest with is [\w][\\](?!\\) however this will get the "\n" from the 1st test string i have listed. I would like to use lookbehind for the regex however javascript does not have such a thing for the version I am using. Here is the site I have been testing my regexs on http://www.regexpal.com/
Currently I am inefficiently using this regex [\w][\\](?!\\) to extract out all single backslashes sandwiched between non-backslash characters and the character before them (which I don't want) and then replacing it with the same string plus a backslash at the end of it.
For example given domain\name\\bl\\\ah my current regex [\w][\\]\(?!\\) will return "n\". This results in my code having to do some additional processing rather than just using replace.
I don't care about any double, triple or quadruple backslashes present, they can be left alone.
For example given domain\name\\bl\\\ah my current regex [\w][\\]\(?!\\) will return "n\". This results in my code having to do some additional processing rather than just using replace.
It will do just using replace, since you can insert the matched substring with $&, see:
console.log(String.raw`domain\name\\bl\\\ah`.replace(/\w\\(?!\\)/g, "$&\\"))
Easiest method of matching escapes, is to match all escaped characters.
\\(.)
And then in the replacement, decide what to do with it based on what was captured.
var s = "domain\\name\\\\backslashesInDoubleBackslashesWontBeReturned";
console.log('input:', s);
var r = s.replace(/\\(.)/g, function (match, capture1) {
return capture1 === '\\' ? match : '$' + capture1;
});
console.log('result:', r);
The closest you can get to actually matching the unescaped backslashes is
((?:^|[^\\])(?:\\\\)*)\\(?!\\)
It will match an odd number of backslashes, and capture all but the last one into capture group 1.
var re = /((?:^|[^\\])(?:\\\\)*)\\(?!\\)/g;
var s = "domain\\name\\\\escapedBackslashes\\\\\\test";
var parts = s.split(re);
console.dir(parts);
var cleaned = [];
for (var i = 1; i < parts.length; i += 2)
{
cleaned.push(parts[i-1] + parts[i]);
}
cleaned.push(parts[parts.length - 1]);
console.dir(cleaned);
The even-numbered (counting from zero) items will be unmatched text. The odd-numbered items will be the captured text.
Each captured text should be considered part of the preceding text.

How to replace last matched character in string using javascript

i want to replace last input character from keyboard to ''
My String Input are
sample string
"<p><strong>abscd sample text</strong></p>"
"<p>abscd sample text!</p>"
My last character is dynamic that can be any thing between
a to z, A to Z, 0 to 9, any special characters([~ / < > & ( . ] ).
So i need to replace just that character
for example in Sample 1 i need to replace "t" and in sample 2 in need to replace "!"
I tried below code. but it id not worked for me
var replace = '/'+somechar+'$/';
Any way to do it?
Step one
to replace the a character in a string, use replace() function of javaScript. Here is the MDN specification:
Returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.
Step two
you need to location the character to be replaced through regular expression. You want to replace the last character of a string and this could be expressed as /(.+)(.)$/. . stands for any character, + means more than one character. Here (.+) matches all the character before the last one. (.) matches the last character.
What you want to replace is the one inside the second brackets. Thus you use the same string matched in the first bracket with $1 and replace whatever after it.
Here is the code to realize your intention:
text = 'abscd sample text';
text.replace(/(.+)(.)$/, '$1!');
Do you really need to use regular expressions? How about str = str.slice(0, -1); ? This will remove the last character.
If you need to replace a specific character, do it like this:
var replace = new RegExp(somechar + '$');
str = str.replace(replace, '');
You cannot use slashes in a string to construct a RegEx. This is different from PHP, for example.
I dont really understand which character you want to replace to what, but i think, you should use replace() function in JS: http://w3schools.com/jsref/jsref_replace.asp
string.replace(regexp/substr,newstring)
This means all keyboard character:
[\t\n ./<>?;:"'`!##$%^&*()[]{}_+=-|\\]
And this way you can replace all keyboard character before < mark to ""
string.replace("[a-zA-Z0-9\t\n ./<>?;:"'`!##$%^&*()[]{}_+=-|\\]<","<")

Categories

Resources