substring/regex to get a src value held in a string

前端 未结 7 1136
花落未央
花落未央 2021-01-02 05:25

I have a string with the value:

var string = \"\'\'
Some plain text
7条回答
  •  太阳男子
    2021-01-02 06:04

    Assuming that you want to match specifically the src property in an tag. You can use this Regex because it will match the tag, still preserve other properties it might have and also other content inside the src property:

    var matches = string.match(/]+)src="[/]?([^"]+)"([^>]*)>|<( *)img( *)[/>|>]/g);

    it can be improved but works fine with some varieties of typing.

    for example, if you want to match or even to add something to the url, you can do the following replacement:

    string.replace(/]+)src="[/]?([^"]+)"([^>]*)>|<( *)img( *)[/>|>]/g, '');

提交回复
热议问题