I'm writing a code to turn dumb quotes to smart quotes:
text.replace(/\b"|\."/g, '”')
(I added OR period because sometimes sentences end with a period not a word.)
Input:
"This is a text."
Output:
“This is a text”
Desired output:
“This is a text.”
As you can see, that code removes the dot.
How to prevent this?
RULES: I want to replace dumb double quotes that are at end of a word or after a period, turn them into right double smart quotes.
You should include in the replacement the capturing group 1 , you can do that with :
replace(/\b"$|(\.)"$/g, "$1”");
$1 Will contain the .
Adding the $ you will avoid miss those cases:
"This is a "text"."
EDIT For the new RULE:
If you want also to replace the internal quotes of a quote do this >
const regex = /( ")([\w\s.]*)"(?=.*"$)|\b"$|(\.)?"$/g;
const str = `"This is a "subquote" about "life"."`;
const subst = `$1$2$3”`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
"You live "your live" always in company"
"You live "alone" always in company"
"You live "" always in company"
"You live "in the dark..." always in company"
"You live "alone" very "alone" always in company"
Please try this:
let text = '"This is a text."'
console.log(
text.replace(/\b"|(\.)"/g,'$1\u201d')
)
Related
I have a string like this:
This is a sentence.\n This is sentence 2.\n\n\n\n\n\n This is sentence 3.\n\n And here is the final sentence.
What I want to is:
This is a sentence.\n This is sentence 2.\n This is sentence 3.\n And here is the final sentence.
I want to remove all duplicated \n characters from a string but keep only one left, is it possible to do like that in javascript ?
You may try replacing \n{2,} with a single \n:
var input = "This is a sentence.\n This is sentence 2.\n\n\n\n\n\n This is sentence 3.\n\n And here is the final sentence.";
var output = input.replace(/\n{2,}\s*/g, '\n');
console.log(output);
You can use regex as /\n+/g to replace it with single \n
const str =
"This is a sentence.\n This is sentence 2.\n\n\n\n\n\n This is sentence 3.\n\n And here is the final sentence.";
const result = str.replace(/\n+/g, "\n");
console.log(result);
I have a string below which has some identifier to get an specific word on it.
string example: "I will c#hec*k on it"
the "#" indicates starting, and the "*" indicates for last.
I want to get two strings.
check - the whole word that has "#" and "*" on it.
hec - string that was surrounded.
I have started to use the below code, but it seems does not work.
sentence.split('#').pop().split('*')[0];
Somebody knows how to do it. would appreciate it thanks
var s = "I will c#hec*k on it"
console.log(s.match(/(?<=#)[^*]*(?=\*)/)) // this will print ["hec"]
console.log(s.match(/\w*#[^*]*\*\w*/).map(s => s.replace(/#(.*)\*/, "$1"))) // this will print ["check"]
where:
(?<=#) means "preceded by a #"
[^*]* matches zero or more characters that are not a *
(?=\*) means "followed by a *"
\w* matches zero or more word characters
(.*) is a capturing group (referenced by $1) matching any number of any kind of character (except for newlines)
I would try something like this with Javascript,
there might be a better approach with regex though.
let sentence = "I will c#hec*k on it";
sentence.split(" ").forEach(word => {
if(word.includes("#") && word.includes("*")){
let betweenChars = word.substring(
word.lastIndexOf("#") + 1,
word.lastIndexOf("*")
)
console.log('Between chars: ', betweenChars);
let withoutChars = word.replace(/[#*]/g,"");
console.log('Without chars: ', withoutChars);
}
});
I have a sentence, and I need to compare it with customer send message and return whether it has been passed or failed.
Sentence contains {#val#}, which can be replaced by any values in the customer send messages. Condition here is in place of {#val#} in a sentence => Customer message can contain anything within the limit of 5.
{#val#} is the dynamic content. Other part of the messages are static content.
In static content of the customer message, we can ignore the space and compare with the sentence defined.
But in dynamic content of the customer send messages,({#val#}) spaces should be considered. For example, sentence contains {#val#}{#val#} and in customer msg it should be replaced by nehradutta not nehra space dutta since 2 {#val#}{#val#}s are continuosly put up in the defined sentence.
var sentence = "Hi {#val#}, Thank you {#val#} {#val#} for visting us"
var customermsg = "Hi asz, Thank you shaky dutta for visting us" //Should Pass as sentence and customer msg are matching
var customermsg1 = "Hiasz, Thank you shaky dutta forvisting us" //Should Pass as sentence and customer msg are matching ( Ignoring the space in static portion )
var customermsg2 = "Hi asz, Thank you nehra dutta for visting us" //Should Fail since there is lot of space between the dynamic content {#val#} => (nehra dutta) places. Should contain single space since the sentence has {#val#}space{#val#}.
I need to form a regular expression, which would avoid spaces while comparing static content and include spaces while comparing dynamic content.
Currently my code is below replacing all the spaces and comparing.
var separators = ['#val#','#VAL#','#vaL#','#vAl#','#Val#'];
var regexStr = sentence.replace(/[\ ]/g,'');
var customermsg = customermsg.replace(/[\ ]/g,'');
separators.forEach(str => {
regexStr = regexStr.replace(new RegExp(str, 'g'), '.{0,5}')
})
var regex = new RegExp(`^${regexStr }$`,"i")
if (!customermsg.match(regex))
Status = "Fail"
else
Status = "Pass"
Kindly help me on this as I'm new to regular expression
var separators = ["#val#", "#VAL#", "#vaL#", "#vAl#", "#Val#"];
let sentence = "Hi {#VAL#}, Thank you {#val#} {#val#} for visting us";
// make all spaces optional. Except "} {".
regexString = sentence.replace(/(^|.)\s($|.)/g, (x, g1, g2) => (x == "} {" ? x : g1 + "\\s?" + g2));
// turn separators into .{0,5}
separators.forEach((str) => {
regexString = regexString.replace(new RegExp(`{${str}}`, "g"), ".{0,5}");
});
// input
var customermsg = "Hi asz, Thank you nehra dutta for visting us"; //Should Pass
var customermsg1 = "Hiasz, Thank you nehra dutta forvisting us"; //Should Pass
var customermsg2 = "Hi asz, Thank you nehradutta for visting us"; //Should Fail
let regex = RegExp("^" + regexString + "$");
console.log("REGEX ==>", regex);
console.log(regex.test(customermsg) ? "Pass" : "Fail", "==>", customermsg);
console.log(regex.test(customermsg1) ? "Pass" : "Fail", "==>", customermsg1);
console.log(regex.test(customermsg2) ? "Pass" : "Fail", "==>", customermsg2);
I had to use two regexes because of the complexity of the conditions but here is my answer.
I explained the regex in comments
const separator = '{#val#}'; // This is what is used to declare dynamic content
const staticSpace = new RegExp(`(?=[^.\\s*${separator}\\s*.])(\\s|.{0})`, 'i');
// This regex called staticSpace, is for those the spacing between static content, that you said should be ignored.
const dynamicSpace = new RegExp(`\\s*(${separator})\\s*`, 'gi');
// This regex called dynamicSpace, is for the possible spacing between static content and dynamic content, that you said shouldn't be too long, so I figured only one space after a dynamic word is alright.
// The \s* is used to mark any amount of space
// The (${seperator}) will input the seperator variable, this part is crucial cus that's how dynamic content would be marked.
// Then the last \s* with the same use to the first
const sentence = "Hi {#val#}, Thank you {#val#} {#val#} for visting us"; // Template for the messages
let customerMessages = ["Hi asz, Thank you shaky dutta for visting us", "Hiasz, Thank you shaky dutta forvisting us", "Hi asz, Thank you nehra dutta for visting us"]; // All the customerMessages
let regex = new RegExp(`^${[].concat.apply([], sentence.split(staticSpace).map(a => a.includes(separator) ? a : a.split(/.{0}|\s/))).filter(e => /\S/.test(e)).join('\\s*').replace(dynamicSpace, '\\s?$1\\s?').replace(new RegExp(separator, 'gi'), '[.\\S]{0,5}')}$`, 'i');
// Firstly, I split the template to the static content to ensure that static spacing is ignored
// Then I replaced all the dynamicSpace with \s?1\s? which is just for one space before and after the seperator
// Then all the extra spacing, this would be static content spacing which you said can be as long as it wants and I replaced it with \s* which matches 0 or as many as possible because of static content that may be joined together
// Finally, I used the seperator to match the dynamic content placeholders. [.\\S]{0,5}, this regex matches any character except a space, with a limit of 5 characters. Because you said that the dynamic content should not be longer than 5 characters
// I passed the flag i to most of the replacements because of case-insensitivity. You woulnd't have to define the same seperator in different cases now
console.log(customerMessages.map(message => ({
message,
state: regex.test(message) ? 'Pass' : 'Fail'
})));
// logging out the result
I have an application where I need to split the string with "," but I want to Ignore "\," ("\\,").
Is there any one line solution?
sample input :- "This is first\\, this is still first, this is second"
sample output :- ["This is first, this is still first", " this is second"]
If you can use a negative look-behind regexp, this could work:
const input = "This is first \\, this is still first, this is second";
// Split on all commas that aren't preceded by a backslash.
const result = input.split( /(?<!\\),/ );
console.log( result );
Just .map() with .trim() if you do not want the leading spaces.
If there's a character sequence that can never appear in the original string, you can replace all the \, with it, split the string, then undo the replacement.
let input = "This is first \\, this is still first, this is second";
let output = input.replace(/\\,/g, '-comma-').split(',').map(s => s.replace(/-comma-/g, ','));
console.log(output);
This isn't a perfect solution if the input is user-generated, since they could type -comma-, but you can make that replacement string arbitrarily complex so it will be improbable.
You can try this
.*?[^\\]+?(?:,|$)
let splitByComma = (str) =>
str.match(/.*?[^\\]+?(?:,|$)/g).map(v => v.replace(/(\\,)|,$/g, (m, g1) => g1 ? ',' : ''))
console.log(splitByComma('\\, some, more some\\123'))
console.log(splitByComma("This is first \\, this is still first, this is second"))
console.log(splitByComma("hello\\,123, some text"))
console.log(splitByComma("\\,\\,\\,123, 123-123\\,1232,"))
I would like to write a javascript regular expression that will match match multi-line strings contained in quotes that possibly also contain quotes. The final quote will be terminated with a comma.
For example:
"some text between the quotes including " characters",
This sting starts with a ", ends with ", and contains " characters.
How do I get this to work?
I guess the real question is How do i match a multi-line string that starts with " and ends with ",??
Doesn't a simple match() work ? You also need to use the \s\S trick to make the dot include line breaks (actually, that makes it accept every single characters ever):
var str = "bla bla \"some text between the quotes \n including \" characters\", bla bla";
var result = str.match(/"([\s\S]+)",/);
if (result == null) {
// no result was found
} else {
result = result[1];
// some text between the quotes
// including " characters
}
Match many non-", or " not followed by ,:
/"((?:[^"]|"(?!,))*)",/
or use lazy quantifier:
/"([\0-\uffff]*?)",/
Using a regular expression will be really tricky, I would try something like this:
var getQuotation = function(s) {
var i0 = s.indexOf('"')
, i1 = s.indexOf('",');
return (i0 && i1) ? s.slice(i0+1, i1) : undefined;
};
var s = "He said, \"this is a test of\n" +
"the \"emergency broadcast\" system\", obviously.";
getQuotation(s); // => 'this is a test of
// the "emergency broadcast" system'