How to split text by regex not in quotation marks - javascript

I am using text.split(' ') to split text by 'space'.
Example:
Hi my name is John
to
['Hi', 'my', 'name', 'is', 'John'];
I would like to ignore spaces in question mark.
Hi pls 'DO NOT SPLIT THIS'
to
['Hi', 'pls', 'DO NOT SPLIT THIS']
How can I do this?
Thank you for any help!

How about the following?
regex = /\s+(?=(?:[^\'"]*[\'"][^\'"]*[\'"])*[^\'"]*$)/g
"Hi pls 'DO NOT SPLIT THIS'".split(regex)
// [ 'Hi', 'pls', "'DO NOT SPLIT THIS'" ]

Related

How to split multiline string into lines?

How to split a multiline string into lines without taking into account the newline in the line itself?
My approach is not working:
const str = `
Hello
World\t
its\nbeautiful
`;
JSON.stringify(str).split(/$\\n/g)
What is the result of this approach:
[""", "Hello", "World\t", "its", "beautiful", """]
What result is needed in the end:
[""", "Hello", "World\t", "its\nbeautiful"]
Since \n is the character that marks new lines, just like a normal new line, there is no way for javascript to differenciate between \n and \n.
An idea for your "special" new lines could be to escape that \n with another \ so you would end up with
const str = `
Hello
World\t
its\\nbeautiful
`;
str.split("\n");
The result would be this:
['', 'Hello', 'World\t', 'its\\nbeautiful', '']

replace some parts for cloze deletion test

For example, I have this text: This is a good reason to stop. (word counts is always greater than 5)
words = ['This', 'is', 'a', 'good', 'reason', 'to', 'stop']
flashcards_count = ciel(len(word) / 3)
The result I'd like to have:
[
'This __ a good ______ to ____.'
'____ is a ____ reason __ stop.'
'This is _ good reason to stop.'
]
As you may notice I'm try to avoid putting blanks in sequence, except if it's the last flashcard.
So for this, I shuffled the words and chunked it into 3 to make each flashcard, but the result might be like this:
[
'This is a ____ ______ __ stop.'
'____ __ _ good reason to stop.'
'This is a good reason to ____.'
]
You can use the random module to shuffle a list of strings, then join them with a space inbewteen :
import random
list = ['This', 'is', 'a', 'good', 'reason', 'to', 'make', 'stop']
random.shuffle(list)
print " ".join(list)
Take a look at Shuffling a list of objects if you want to shuffle non-strings aswel.

Extract hashtags from complex string using regex

I have a crazy string, something like:
sun #plants #!wood% ##arebaba#tey travel#blessed #weed das#$#F!#D!AAAA
I want to extract all "words" (also containing special characters) that begin with # or that have a space right before, taking the following as a result:
[
'sun',
'plants',
'!wood%',
'arebaba',
'tey',
'travel',
'blessed',
'weed',
'das',
'$',
'F!#D!AAAA'
]
How do I get this using regex?
You can use match using regex: [^#\s]+:
var str = 'sun #plants #!wood% ##arebaba#tey travel#blessed #weed das#$#F!#D!AAAA';
var arr = str.match(/[^\s#]+/g);
console.log(arr);
RegEx Demo
Just using match you could get all the group 1 matches into an array.
(?:^|[ #]+)([^ #]+)(?=[ #]|$)
Easy!
(?: ^ | [ #]+ )
( [^ #]+ ) # (1)
(?= [ #] | $ )
Or, if you feel it's this simple, then just use ([^ #]+) or [^ #]+
which gets the same thing (like split in reverse).

get any string between last two slash or other last two same character with regex?

how to get any string between last two slash or other last two same character in javascript?
use regex not split there's several similar question in so but I only can find answer is use split ...
my regex pattern in below, it not match do I miss something?
I was hoping the result is something like this, how to make it?
['s', index: .., input: ...]
regex
var str = '/a/b/c/s/';
var regexPattern = /([^/]*)\/$/;
str = regexPattern.exec(str);
console.log(str); // ["s/", "s"]
if (str == 's') {
console.log(true)
}
https://jsfiddle.net/30bjt5ew/
You can use this regex:
/[^/]*(?=\/$)/
it will output ["s", index: 7, input: "/a/b/c/s/"] as you expected.
[^/]* # any char that is not /
(?=\/$) # Look foward for a / and the end of string
jsfiddle

.split() on elements of a sentence string, advanced separator

I want to be able to split a sentence string into an array of individual word strings.
sentenceArr = 'I take the dog to the park'
sentenceArr.split(' ');
Desired result: ['I', 'take', 'the', 'dog', 'to', 'the', 'park']
This is easy if they are just split by spaces as above, but if there are commas or double spaces, or RegExes in the string it can come unstuck.
sentenceArr = 'I take,the dog to\nthe park'
sentenceArr.split(' ');
How can I modify the split() separator argument to account for these irregularities?
Ideally, I want to be able to split anywhere there isn't a letter.
split also takes a regex as argument :
sentenceArr = 'I take,the dog to\nthe park'
var r= sentenceArr.split(/\W+/);
console.log(r)

Categories

Resources