Regex - Get x number of characters from a string but html tags does not count towards the count
up vote
2
down vote
favorite
As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.
Example string
Some <i>t</i>est <b>string</b> test
9 characters would output
Some <i>t</i>est
13 characters would output
Some <i>t</i>est <b>str
19 characters would output
Some <i>t</i>est <b>string</b> te
I'm not too sure where to go from the basic
(.*?){0,10}
Also, is there a way to make HTML encoded characters e.g. <
count as one character?
javascript regex
|
show 1 more comment
up vote
2
down vote
favorite
As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.
Example string
Some <i>t</i>est <b>string</b> test
9 characters would output
Some <i>t</i>est
13 characters would output
Some <i>t</i>est <b>str
19 characters would output
Some <i>t</i>est <b>string</b> te
I'm not too sure where to go from the basic
(.*?){0,10}
Also, is there a way to make HTML encoded characters e.g. <
count as one character?
javascript regex
Try^(?:(?:<[^<>]*>)*[^<]){9}
, see demo.
– Wiktor Stribiżew
Nov 19 at 11:51
That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. < count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 at 12:15
Like this?^(?:(?:<[^<>]*>|<.*?>)*.){9}
– Wiktor Stribiżew
Nov 19 at 12:17
Very close I've changed it a little to^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21}
but I want the &.*; to count as one letter. any ideas?
– Adam Buckley
Nov 19 at 12:56
I've just worked it out^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21}
regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
– Adam Buckley
Nov 19 at 13:03
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.
Example string
Some <i>t</i>est <b>string</b> test
9 characters would output
Some <i>t</i>est
13 characters would output
Some <i>t</i>est <b>str
19 characters would output
Some <i>t</i>est <b>string</b> te
I'm not too sure where to go from the basic
(.*?){0,10}
Also, is there a way to make HTML encoded characters e.g. <
count as one character?
javascript regex
As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.
Example string
Some <i>t</i>est <b>string</b> test
9 characters would output
Some <i>t</i>est
13 characters would output
Some <i>t</i>est <b>str
19 characters would output
Some <i>t</i>est <b>string</b> te
I'm not too sure where to go from the basic
(.*?){0,10}
Also, is there a way to make HTML encoded characters e.g. <
count as one character?
javascript regex
javascript regex
edited Nov 19 at 13:28
Wiktor Stribiżew
304k16123200
304k16123200
asked Nov 19 at 11:48
Adam Buckley
184
184
Try^(?:(?:<[^<>]*>)*[^<]){9}
, see demo.
– Wiktor Stribiżew
Nov 19 at 11:51
That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. < count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 at 12:15
Like this?^(?:(?:<[^<>]*>|<.*?>)*.){9}
– Wiktor Stribiżew
Nov 19 at 12:17
Very close I've changed it a little to^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21}
but I want the &.*; to count as one letter. any ideas?
– Adam Buckley
Nov 19 at 12:56
I've just worked it out^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21}
regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
– Adam Buckley
Nov 19 at 13:03
|
show 1 more comment
Try^(?:(?:<[^<>]*>)*[^<]){9}
, see demo.
– Wiktor Stribiżew
Nov 19 at 11:51
That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. < count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 at 12:15
Like this?^(?:(?:<[^<>]*>|<.*?>)*.){9}
– Wiktor Stribiżew
Nov 19 at 12:17
Very close I've changed it a little to^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21}
but I want the &.*; to count as one letter. any ideas?
– Adam Buckley
Nov 19 at 12:56
I've just worked it out^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21}
regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
– Adam Buckley
Nov 19 at 13:03
Try
^(?:(?:<[^<>]*>)*[^<]){9}
, see demo.– Wiktor Stribiżew
Nov 19 at 11:51
Try
^(?:(?:<[^<>]*>)*[^<]){9}
, see demo.– Wiktor Stribiżew
Nov 19 at 11:51
That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. < count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 at 12:15
That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. < count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 at 12:15
Like this?
^(?:(?:<[^<>]*>|<.*?>)*.){9}
– Wiktor Stribiżew
Nov 19 at 12:17
Like this?
^(?:(?:<[^<>]*>|<.*?>)*.){9}
– Wiktor Stribiżew
Nov 19 at 12:17
Very close I've changed it a little to
^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21}
but I want the &.*; to count as one letter. any ideas?– Adam Buckley
Nov 19 at 12:56
Very close I've changed it a little to
^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21}
but I want the &.*; to count as one letter. any ideas?– Adam Buckley
Nov 19 at 12:56
I've just worked it out
^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21}
regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.– Adam Buckley
Nov 19 at 13:03
I've just worked it out
^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21}
regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.– Adam Buckley
Nov 19 at 13:03
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
To match a certain amount of chars from the string start excluding substrings like <...>
from the count and counting &XX*;
entities as one unit, you may use
^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}
See the regex demo.
Details
^
- start of string
(?:
- start of a non-capturing group that will serve as a containter for the alternatives below:
&[^s;]*;
- a&
, 0 or more chars other than whitespace and;
and then;
|
- or
(?:<[^<>]*>)*.
- 0 or more repetitions of<
, 0+ chars other than<
and>
and then>
, and then any char other than a line break char (use DOTALL modifier to match any char with.
)
){20}
- end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
To match a certain amount of chars from the string start excluding substrings like <...>
from the count and counting &XX*;
entities as one unit, you may use
^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}
See the regex demo.
Details
^
- start of string
(?:
- start of a non-capturing group that will serve as a containter for the alternatives below:
&[^s;]*;
- a&
, 0 or more chars other than whitespace and;
and then;
|
- or
(?:<[^<>]*>)*.
- 0 or more repetitions of<
, 0+ chars other than<
and>
and then>
, and then any char other than a line break char (use DOTALL modifier to match any char with.
)
){20}
- end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.
add a comment |
up vote
1
down vote
accepted
To match a certain amount of chars from the string start excluding substrings like <...>
from the count and counting &XX*;
entities as one unit, you may use
^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}
See the regex demo.
Details
^
- start of string
(?:
- start of a non-capturing group that will serve as a containter for the alternatives below:
&[^s;]*;
- a&
, 0 or more chars other than whitespace and;
and then;
|
- or
(?:<[^<>]*>)*.
- 0 or more repetitions of<
, 0+ chars other than<
and>
and then>
, and then any char other than a line break char (use DOTALL modifier to match any char with.
)
){20}
- end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
To match a certain amount of chars from the string start excluding substrings like <...>
from the count and counting &XX*;
entities as one unit, you may use
^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}
See the regex demo.
Details
^
- start of string
(?:
- start of a non-capturing group that will serve as a containter for the alternatives below:
&[^s;]*;
- a&
, 0 or more chars other than whitespace and;
and then;
|
- or
(?:<[^<>]*>)*.
- 0 or more repetitions of<
, 0+ chars other than<
and>
and then>
, and then any char other than a line break char (use DOTALL modifier to match any char with.
)
){20}
- end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.
To match a certain amount of chars from the string start excluding substrings like <...>
from the count and counting &XX*;
entities as one unit, you may use
^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}
See the regex demo.
Details
^
- start of string
(?:
- start of a non-capturing group that will serve as a containter for the alternatives below:
&[^s;]*;
- a&
, 0 or more chars other than whitespace and;
and then;
|
- or
(?:<[^<>]*>)*.
- 0 or more repetitions of<
, 0+ chars other than<
and>
and then>
, and then any char other than a line break char (use DOTALL modifier to match any char with.
)
){20}
- end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.
answered Nov 19 at 13:27
Wiktor Stribiżew
304k16123200
304k16123200
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374009%2fregex-get-x-number-of-characters-from-a-string-but-html-tags-does-not-count-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
^(?:(?:<[^<>]*>)*[^<]){9}
, see demo.– Wiktor Stribiżew
Nov 19 at 11:51
That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. < count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 at 12:15
Like this?
^(?:(?:<[^<>]*>|<.*?>)*.){9}
– Wiktor Stribiżew
Nov 19 at 12:17
Very close I've changed it a little to
^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21}
but I want the &.*; to count as one letter. any ideas?– Adam Buckley
Nov 19 at 12:56
I've just worked it out
^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21}
regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.– Adam Buckley
Nov 19 at 13:03