I was wondering how to match and replace an odd amount of slashes (\) in every line at javascript.
They are used in escaping a string, but sometimes the string is wrapped into lines, so the slash have to move to the next line.
Here is an example: http://regex101.com/r/iI9vO9
I want to match the lines which are marked via "Yes" and ignore the lines marked with "No".
For Example:
"Yes 1\" +
"No 2\\" +
"Yes 3\\\" +
"No 4\\\\" +
"No"
Should be changed to:
"Yes 1" +
"\No 2\\" +
"Yes 3\\" +
"\No 4\\\\" +
"No"
Notice there is characters before and after the slashes in each line, and the slash is moved to the next line when it is repeated an odd time.
I couldn't get it working with (\\)(\\\\)* or look-around.
This is what I have in mind if this work:
text.replace(/([^\\])\\" \+ \n"(.)/gm, '$1\\$2"+ \n "')
If this is not possible with RegEx, I would appreciate any other way to make this possible.
Thanks for your help.
EDIT:
For whoever look this up on Google, this is exactly what solves the problem:
text.replace(/([^\\])((\\{2})*)\\" \+ \n"/g, '$1$2" + \n"\\')
http://jsfiddle.net/5mGWF/1/
This seems to do what you want:
text = text.replace(/([^\\])((\\{2})*)\\\n/g, "$1$2\n\\")
http://jsfiddle.net/5mGWF/
Related
I would like to have a specific text colored differently in HTML/PHP/CSS/JS.
I have a text similar to this structure:
#john please move the invite to the next week.
What i would like to achieve is to have the wording "#john" colored in green while the rest of the text characters should remain in black. In other words the "#" and the first space " " right after the #, are the delimiters of the green text.
Can you please advise on how to achieve this?
Thanks,
Slim
You can use Regular expressions for this; the character \# will just match a "#", and the character \w+ will match a word - a word is a set of characters which are unseparated by commas, spaces, full stops, etc.
Lastly, you will need to use a String Iterator to find all matches and loop through them, and, of course, re-color them.
Here is how you would do this:
function color_text(text) {
let matches = text.matchAll(/\#\w+/g);
let current_match;
while(!current_match?.done) {
current_match = matches.next();
if(current_match?.done) {
break;
}
let value = current_match?.value[0];
text = text.replace(value, "<span style='color: #00db58'>" + value + "</span>");
}
return text;
}
// TESTING HERE -- - - -- - - -
document.write(color_text("#john please move the invite to the next week."));
document.write("<br>");
document.write(color_text("#john please tell #josh to stop being annoying."));
I'm trying to match the full word "and" EXCEPT when it appears in idioms that repeat the same word before and after it, like "more and more" or "again and again". I've got this:
/(\b\w*\b)\s\band\s(?!\1)/gi
Which works, except it also captures the word before "and." I understand you can't do lookbehind regex in JS... Any help appreciated!
EXAMPLE:
It should match this and plus this and but not more and more or less and less.
would it be possible for you to test the opposite, test for the existence of matching pairs and negate the result?
/(\b\w+\b)\s\band\s(\1)/gi
I did a quick few tests on this at https://regex101.com/r/ZqXbtL/1/tests and it gave me the results I would expect with negation.
i think its better to get the index of matched items and add the length of the word and the space after it to get to index of "and", and so you can use substring or anything like that, i know its not the exact thing you want , but maybe it helps .
the snippet below will get index of second "and"
edit: got help from this answer
var reg = /(\b\w*\b)\s(?=\band\s(?!\1))/g;
var match,indexes = [];
while(match = reg.exec(document.body.innerHTML)){
indexes.push(match.index + match[0].length);
}
// testing
indexes.forEach(function(element){
document.body.innerHTML += "<br>\"" +
document.body.innerHTML.substr(element,3) + "\" found at " + element;
});
test and test<br>
test and not
I have a javascript that should detect hashtags and mentions and replace them. I am using this library: https://github.com/AnSavvides/jquery.linky
function _linkifyHashtags(text, links) {
// If there is no search URL for a hashtag, there isn't much we can do
if (links.hashtagSearchUrl === null) return text;
return text.replace(/(^|\s|\(|>)#((\w|[\u00A1-\uFFFF])+)/g, "$1<a href='" + links.baseUrl + links.hashtagSearchUrl + "$2' target='_blank'>#$2</a> ");
}
The problem with this code is that the regEx only work when hashtags is written with spaces before (Like: "#hi #hello #lo")
If the hashtags is written with no space before, like this "#hi#hello#lo", the script is not detecting the hashtags.
I have tried to add possible none white character before also:
/(^|\s|\S|\(|>)#((\w|[\u00A1-\uFFFF])+)/
It works "half way". Suddenly every second hashtag with no space before becomes detected by the script.
I am no RegEx expert and have tried to read up on this, but I can't se how to get this part right. Anyone who knows?
Just remove the limitation on what can precede a tag as nothing is also a valid alternative now.
/#((\w|[\u00A1-\uFFFF])+)/g
You should also remove it from the replace accordingly, simplifying it to:
return text.replace(/#((\w|[\u00A1-\uFFFF])+)/g, "<a href='" + links.baseUrl + links.hashtagSearchUrl + "$1' target='_blank'>#$1</a> ");
Try this :
/(^|\s?|\(|>)#((\w|[\u00A1-\uFFFF])+)/
The ? after \s catches 0 or 1 whitespaces.
hey try this regular expression.
/(\s|\w)*[\#][\w]*/g
Regex fiddle
Text To Remove:
I am trying to remove a line of text from a file.
Everything between <%'/testStart'%> and <%'/testEnd'%>` including the delimiters.
<%'/testStart'%> Some text with other random characters in between <%'/testEnd'%>
JavaScript:
I have tried this with no luck. Well, at one point I had it working with everything hard coded in the RegExp. But I have tried so many ways I can't remember what I did. Basically I think I am just not escaping something properly.
var p = "/test"; //this is dynamic
var start = "<%'" + p + "Start" + "'%>";
var end = "<%'" + p + "End" + "'%>";
var regex = new RegExp("\\" + start + "[^:]\\" + end);
var newData = data.replace(regex,"");
Expected Result:
Completely remove this line.
<%'/testStart'%> Some text with other random characters in between <%'/testEnd'%>
Any help is much appreciated. Thanks,
Match Inside with a Lazy .*?
Replace your inner section "[^:]\\" with ".*?"
var regex = new RegExp( start + ".*?" + end );
The effect is to match everything up to the end parameter.
Explanation
The star quantifier in .*? is made "lazy" by the ? so that the dot only matches as many characters as needed to allow the next token to match (shortest match). Without the ?, the .* first matches the whole string, then backtracks only as far as needed to allow the next token to match (longest match).
var shortLocString = locationString;
console.log("Index of dash in '" + shortLocString + "': " + shortLocString.indexOf('-'));
prints this every time:
Index of dash in '325–333 Avenue C, New York, NY': -1
Why do I feel like this is some stupid thing I'm missing at the end of a long day?
Thanks for the help!
Mike
You have some sort of not-hyphen character in your string. I'm not sure which one; there are a bunch that look like that.
There are many hyphen-like characters available.
If you type in "-", it might not be the same as the character which is being put in the string you're testing.
If you have the string you want to test (ie: that one), an easy solution might be to copy the dash which exists inside of the string, paste it into your .indexOf and try again.
For a more-robust solution, get the Unicodes for all hyphens (include the actual minus sign), and if you're dealing with phone-numbers or serial-numbers which have to be formatted the same way, every time, then manually do a RegEx replace of any of those dashes, into the format you will use for your checks.
Also, be prepared for headaches with MS Word or other rich-text editors.
They will swap your " for fancy quotes in a heartbeat, and then all of your strings are broken. They may also use different character-mappings for accents, et cetera.
It's because there's more than one type of dash - you're checking for a hyphen, but the address contains an en dash.
console.log("Index of dash in '" + shortLocString + "': " + shortLocString.indexOf('\u2013'));
should print
Index of dash in '325–333 Avenue C, New York, NY': 3
If your addresses are likely to include em and/or en dashes you'll want to check for the characters \u2013 and \u2014